Monday, 17 November 2014

Unlock Bulk AD Users From CSV using Powershell script

In this article, I am going write Powershell script samples to 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>

Unlock Active Directory Users from Specific OU

The below powershell script unlock all the locked-out users from TestOU, you can add your own filter criteria to select users to unlock.
Import-Module ActiveDirectory
Get-ADUser -Filter 'Name -like "*"' `
  -SearchBase "OU=TestOU,DC=TestDomain,DC=local" | Unlock-ADAccount

Unlock Bulk AD Users from CSV file using Powershell Script

   1. Consider the CSV file LockedOutUsers.csv (Ex file: Download ADUsers.csv) which contains set of Locked-out Active Directory users to unlock with the attribute 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:\Scripts\LockedOutUsers.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 from Powershell console to Unlock Bulk Active Directory users from CSV file.


No comments:

Post a Comment