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

thanks,
Elliott

$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.SetPassword($password)
         $user_obj.SetInfo()
         $user_obj.Put(“description”, “$username”)
         $user_obj.SetInfo()
         $user_obj.psbase.invokeset(“AccountDisabled”, “False”)
         $user_obj.SetInfo()
   [ADSI]$account = "WinNT://$computer/Admin,user"
   [ADSI]$group = "WinNT://$computer/Administrators,Group"
         $group.add($account.path)
         $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

LVL 1
Elliott ClarkDBAAsked:
Who is Participating?
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.

MAS (MVE)EE Solution GuideCommented:
This command will unlock all locked accounts.
Search-ADAccount -LockedOut | Unlock-ADAccount

Open in new window

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

yes this is local accounts across our environment
0
Shaun VermaakTechnical Specialist/DeveloperCommented:
Is the password in the actual script you run complex?
0
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.
0
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
Powershell

From novice to tech pro — start learning today.