PowerShell to Unlock local accounts

Hi Experts,

I run a PowerShell script to create local admin accounts across our domain.

when the accounts are created they are created 'Locked'

Can you please help with what the command is to A: create them 'unlocked'
and B: the command to unlock local accounts via PowerShell, as I need to run a separate script to unlock them as my script will skip the account if it is already created.

I have attached my script for reference


$username = "Admin"
$password = "Password"
$computers = Get-Content C:\temp\serverlist.txt
Foreach ($computer in $computers) {
   $users = $null
   $computer = [ADSI]“WinNT://$computer”
   Try {
      $users = $computer.psbase.children | select -expand name  
      if ($users -like $username) {
         Write-Host "$username already exists"
      } Else {
         $user_obj = $computer.Create(“user”, “$username”)
         $user_obj.Put(“description”, “$username”)
         $user_obj.psbase.invokeset(“AccountDisabled”, “False”)
   [ADSI]$account = "WinNT://$computer/Admin,user"
   [ADSI]$group = "WinNT://$computer/Administrators,Group"
         $users = $computer.psbase.children | select -expand name
         if ($users -like $username) {
            Write-Host "$username has been created on $($computer.name)"
         } Else {
            Write-Host "$username has not been created on $($computer.name)"
   } Catch {
      Write-Host "Error creating $username on $($computer.path):  $($Error[0].Exception.Message)"

Open in new window

Elliott ClarkDBAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MASEE Solution Guide - Technical Dept HeadCommented:
This command will unlock all locked accounts.
Search-ADAccount -LockedOut | Unlock-ADAccount

Open in new window

Senior IT System EngineerIT ProfessionalCommented:
thanks for sharing the script.
I believe it is for the local account in each of the specific servers ?
Elliott ClarkDBAAuthor Commented:
Hi both,

yes this is local accounts across our environment
Shaun VermaakTechnical SpecialistCommented:
Is the password in the actual script you run complex?
Elliott ClarkDBAAuthor Commented:
Hi Shaun,

no the password is set as " Password123** "

I then run an additional script which sets the password complex and is not displayed in plain text anywhere or cached.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.