Here are two PowerShell scripts to reset the local administrator password on multiple machines. The first one seaches an OU with a filter on objectClass=Computer and prompts for a new password. The second script you use a text file imported with computer names which the script reads from. Either way works, I prefer the first.
## This variable creates the LDAP connection to Servers, you must enter in the correct DN name of the OU you will be using.
$objDomain = [adsi] (“LDAP://OU=Servers,OU=OU,dc=domain,dc=local”)
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = ‘(objectClass=Computer)’
#$objsearch.propertiestoload.add(“name”)
$colResults = $objSearcher.FindAll()
$password = read-host “Please type in the new password.”
$erroractionpreference = “SilentlyContinue”
foreach ($Computer in $colResults) {
$ServerName = $($Computer.properties.name).ToUpper()
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($ServerName)
if($Reply.status -eq “success”) {
Write-Host “$ServerName is online”
$Admin=[adsi](“WinNT://” + $ServerName + “/administrator, user”)
$Admin.PSBase.Invoke(“SetPassword”, $password)
# Verify password was just changed
$PasswordAge = $Admin.PasswordAge
If($PasswordAge -ne $null) {
Write-Host “$ServerName password change SUCCEEDED”
} Else {
Write-Host “$ServerName password change FAILED”
}
} Else {
Write-Host “$ServerName is not online – skipping”
}
}
#This script will change the local adminstrator password for every machine in serverlist.txt
#Only if the server is online via ping request
$erroractionpreference = “SilentlyContinue”
##for <directorylocal> type in the correct location of the serverlist.txt
foreach ($Computer in get-content <directorylocal>\serverlist.txt) {
$ServerName = $Computer.ToUpper()
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($Computer)
if($Reply.status -eq “success”) {
Write-Host “$ServerName is online”
$Admin=[adsi](“WinNT://” + $Computer + “/administrator, user”)
##replace –-password—with the actual password
$Admin.PSBase.Invoke(“SetPassword”, “–password–”)
# Verify password was just changed
$PasswordAge = $Admin.PasswordAge
If($PasswordAge -ne $null) {
Write-Host “$ServerName password change SUCCEEDED”
} Else {
Write-Host “$ServerName password change FAILED”
}
} Else {
Write-Host “$ServerName is not online – skipping”
}
}