Unlocking AD accounts automatically using PowerShell

Step 1: Identify locked accounts

Retrieve a list of locked-out AD accounts.

Search-ADAccount -LockedOut | Select-Object Name, SamAccountName

Step 2: Unlock specific AD accounts

To manually unlock a specific user account using their SamAccountName, run the script below by replacing "johndoe" with the actual username.

Unlock-ADAccount -Identity "johndoe"

Step 3: Unlock all locked accounts

To unlock all locked accounts in the domain, run the script below.

To unlock all locked accounts in the domain, run the script below.

Step 4: Automate the unlock process with a scheduled task

To automatically unlock accounts every hour, save the script below as UnlockAccounts.ps1.

Search-ADAccount -LockedOut | Unlock-ADAccount

After this, create a scheduled task to run it periodically. This schedules the script to run at midnight daily, but you can modify it as needed.

$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\UnlockAccounts.ps1" $Trigger = New-ScheduledTaskTrigger -Daily -At 12:00AM Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "AutoUnlockADAccounts" -Description "Automatically unlocks AD accounts"

FAQs

1. How do I manually unlock a user’s AD account?

Unlock a user’s AD account using the script below.

Unlock-ADAccount -Identity username

2. Can I unlock all locked-out users at once?

Yes, run the script below to unlock all locked-out users at once.

Search-ADAccount -LockedOut | Unlock-ADAccount

3. How can I automate account unlocks?

Schedule the unlock script to run periodically via Task Scheduler.

 
  • Step 1: Identify locked accounts
  • Step 2: Unlock specific AD accounts
  • Step 3: Unlock all locked accounts
  • Step 4: Automate the unlock process with a scheduled task
  • FAQs

ADSelfService Plus trusted by

A single pane of glass for complete self service password management
Email Download Link