I am trying to run the powershell code below to rename the local administrator username and then set a password.
The scipt runs successfully but is unable to rename the Administrator account or set the password. It just returns the error message from the code below
Error Renaming Administrator Account on TestMachine2008
Error When Setting Password TestMachine2008
What am I doing wrong?
$securePW = Read-Host -assecurestring “Please Enter The New Local Admin Password”
$tempCred = New-Object System.Management.Automation.PSCredential(“Temp”,$securePW)
$newcred = $tempCred.GetNetworkCredential()
$userPW = $newcred.Password.ToString()
$computers = @(“TestMachine2008”)
foreach($computer in $computers) {
if (test-connection -computername $computer -quiet) {
try {
$WinNTPath = “WinNT://” + $computer + “/Administrator,User”
$localAdmin = [ADSI]$WinNTPath
try {
$localAdmin.psbase.rename(“SuperAdmin”)
Write-Host "Successfully Renamed Administrator Account on " $computer
}
catch {
Write-Host "Error Renaming Administrator Account on " $computer
}
$localAdmin.setpassword($userPW)
Write-Host "Successfully Set Password on " $computer
}
catch {
Write-Host "Error When Setting Password " $computer
}
}
else {
Write-Host “Ping Failed to” $computer
}
}