• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1915
  • Last Modified:

PowerShell Script for unlocking AD accounts

We are looking for a power shell script that we can use to unlock AD user accounts. Ideally we would like something that would prompt us for the SamAccountName of which user to unlock.

Any assistance would be appreciated.
0
GR JN
Asked:
GR JN
  • 6
  • 4
  • 3
1 Solution
 
SubsunCommented:
You can use Unlock-ADAccount command in PowerShell. Refer the TechNet Article for details.

http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/05/weekend-scripter-active-directory-account-unlock-shortcut-for-help-desk.aspx
0
 
becraigCommented:
yup for your script a simple Read-host would do
$user = Read-Host "Enter user account to unlock"
Search-ADAccount -LockedOut | Where {$_.samaccountname -eq $user} | Unlock-ADAccount

Open in new window

This would take the sam account name as input
0
 
SubsunCommented:
Simple Unlock-ADAccount  $user should work.. not need to use Search-ADAccount
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
becraigCommented:
only added user for validation.. not need to add time but makes sense to validate input ? (or maybe not ?)
0
 
SubsunCommented:
IMO Search-ADAccount will be time consuming (if we care about time.. :-) ).. Using Try Catch might be more quick for validation. or may be Get-ADUser..
Try { Unlock-ADAccount  $user } Catch { $_.exception }

Open in new window

0
 
SubsunCommented:
@Narvaezj, I did provide similar solution in the first comment. Any reason for accepting becraig's comment as the sole answer?

Except from technet Article..
@echo off
powershell.exe -Command "& {Import-Module ActiveDirectory; Read-Host "Enter the user account to unlock" | Unlock-ADAccount}"

Open in new window

1
 
GR JNAuthor Commented:
As both options were most likely "correct", we utilized becraig's by default.
0
 
becraigCommented:
Narvaezj can you please give the points to Subsun (whether you went with my solution or not), I am only here to help and don't want to step on the toes of the other experts by getting points they think should be awarded to them .
0
 
GR JNAuthor Commented:
No problem. I just do not have the resources to test every solution that is posted.
0
 
SubsunCommented:
@becraig, It's not about points. as the solutions are apparently same, I wanted to understand why the first comment was ignored. As you know the Experts are not getting any special credits for points or answers. We all are here to learn and help not for any other monetary benefits..
0
 
becraigCommented:
Agreed, I usually just leave it as is where a viable solution was awarded points even if it echoed mine, since anyone searching in the future would at least find a working solution.


I generally take issue when a non-answer is tagged as an answer (no help to anyone who come later since the first thing you scan for the is "accepted solution") :~)
0
 
GR JNAuthor Commented:
Yes, that is fine.
0
 
SubsunCommented:
@Mike, As OP stated he didn't try the first suggestion, I am OK with with current selection of solution. I just wanted to know if OP had any trouble using my suggestion.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now