Unlock AD User Account using Powershell script

In this article, I am going write Powershell script samples to unlock Active Directory user account by user’s samAccountName and unlock set of AD Users from specific OU, and unlock bulk AD users from CSV file using Powershell script.
You can unlock an AD User Account by using Active Directory Powershell cmdlet Unlock-ADAccount.

Unlock-ADAccount -Identity <adaccount>

Summary:

Unlock AD User Account by samAccountName

Import-Module ActiveDirectory
Unlock-ADAccount -Identity Morgan

Unlock Active Directory Users from specific OU

Import-Module ActiveDirectory
Get-ADUser -Filter 'Name -like "*"' `
  -SearchBase "OU=Austin,DC=TestDomain,DC=local" | Unlock-ADAccount

Unlock bulk AD Users from CSV file using Powershell Script

   1. Consider the CSV file LockedOutUsers.csv which contains set of Locked-out Active Directory users to unlock with the column header  samAccountName.

Unlock Bulk AD Users from CSV file using Powershell script

   2. Copy the below Powershell script and paste in Notepad file.
   3. Change the LockedOutUsers.csv file path with your own csv file path.
   4. SaveAs the Notepad file with the extension .ps1 like Unlock-Bulk-AD-Users-FromCSV.ps1

Powershell Script: Download Unlock-Bulk-AD-Users-FromCSV.ps1

Import-Module ActiveDirectory
Import-Csv "C:\ScriptsLockedOutUsers.csv" | ForEach-Object {
 $samAccountName = $_."samAccountName" 
Get-ADUser -Identity $samAccountName | Unlock-ADAccount
Write-Host "-User "$samAccountName" unlocked"
}

   5. Now run the Unlock-Bulk-AD-Users-FromCSV.ps1 file in Powershell console to unlock bulk Active Directory users from CSV file.

Advertisement

Leave a Comment