powershell script to unlock local accounts

I am looking for code examples for powershell to find and then unlock local accounts (not AD accounts)

Thanks!
LVL 1
SquigglyMonkeyAsked:
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.

Vasil Michev (MVP)Commented:
0
SquigglyMonkeyAuthor Commented:
I found this earlier today, it was written by someone else, or copied by one of them, it's almost identical. After loading the module I can't get the 'enable' to actually enable.
0
SubsunCommented:
Try..
$Username = "TestUser"
$Computer = "ServerA"
[ADSI]$user = [ADSI] "WinNT://$Computer/$Username"
If ($user.IsAccountLocked -ne $null){
	IF ($user.IsAccountLocked -ne $false) {
		Try {
		$user.IsAccountLocked = $False
		$user.SetInfo()
		"Unlocked the account $Username"
		}
		Catch {
		"Error whle Unlocking the account $Username : $($_.Exception.Message)"
		}
	}
	Else{
	"The account $Username is not locked"
	}
}
Else{
"Unable to find user $Username"
}

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

SquigglyMonkeyAuthor Commented:
Thanks Subsun, I am not sure that is the path I want to go down.
I need to run a script nightly, find locked accounts and unlock them. All of them will be local accounts (not Domain accounts) on a single server.

This is the code I was working on to accomplish it, using the local admin modules from Microsoft:

Get-LocalUser | Where-Object {$_.LockedOut -eq $True} | unlock-LocalUser
0
SubsunCommented:
I never used this module so I am not sure if it works.. You can also use WMI to do this task..
0
SubsunCommented:
Here is a sample code which will unlock all locked local accounts in computer..
Get-WmiObject -Class Win32_UserAccount | 
 ?{$_.LocalAccount -eq $True -and $_.Lockout -eq $True}| 
 % {
 $User = $_
 $User.Lockout = $false
 $User.Put()
}

Open in new window

0
SquigglyMonkeyAuthor Commented:
Get-WmiObject -Class Win32_UserAccount
This pulls every name in the Active Directory.... that's like 10K+
0
SubsunCommented:
You need to add filter..
Get-WmiObject -Class Win32_UserAccount | 
 ?{$_.LocalAccount -eq $True -and $_.Lockout -eq $True}

Open in new window

0
SquigglyMonkeyAuthor Commented:
Well, when I run it, with the filter, it chugs for like 10 minutes, till I kill it.
So when I ran the first line alone, I could see it was loading thousands of accounts.
0
SubsunCommented:
I just tried it and it works for me.. Did you try it on any other server and see if it shows same issue?
0
SubsunCommented:
Here is ADSI method to unlock all locked local accounts..
Foreach ($user in (([ADSI]"WinNT://.").psbase.children | 
	? {$_.psbase.schemaClassName -match "user" -and $_.IsAccountLocked -eq $true}))
{
	$user.IsAccountLocked = $False
	$user.SetInfo()
}

Open in new window


To list locked accounts..
([ADSI]"WinNT://.").psbase.children | 
	? {$_.psbase.schemaClassName -match "user" -and $_.IsAccountLocked -eq $true}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SquigglyMonkeyAuthor Commented:
That works great! Thank you.
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
Shell Scripting

From novice to tech pro — start learning today.