Home > other >  Creating a powershell script to unlock AD accounts
Creating a powershell script to unlock AD accounts

Time:01-26

I am new to Powershell, I am trying to create a fast script that I can run as admin with one click of a button to display the current accounts locked out first and then have a pre written text in the command line, in which I can just type the SAM account name I want to unlock, since I don't want to unlock them all at once necessarily.

My question is, how can I get PS to run a command and then pre write text into command line for the to fill the rest in and execute?

Search-ADAccount -lockedout | Select-Object Name, SamAccountName
Unlock-ADAccount -Identity samAccountName

Thanks!

CodePudding user response:

Read-host is one option when working interactively.

e.g.

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

$samAccountName = Read-Host -Prompt 'Enter the samAccountName of the account you wish to unlock'

if($samAccountName) {
    Unlock-ADAccount -Identity $samAccountName
}

CodePudding user response:

in order to prompt a text for the users to be unlocked, you can use the read-host command:

and you can unlock multiple accounts at once as follow:

Search-ADAccount -lockedout | Select-Object Name, SamAccountName
$Samaccountnames = Read-Host "Enter SamAccountNames of accounts to unlock separated by comma ','"
$Samaccountnames =$Samaccountnames.split(",")
$Samaccountnames | % {Unlock-ADAccount -Identity $_}

CodePudding user response:

this command will unlock all AD users in a domain.

if you want o add confirnation add -Confirm

Search-ADAccount -Lockedout | Unlock-AdAccount -Confirm

if not:

Search-ADAccount -Lockedout | Unlock-AdAccount

this command will final all locked user account:

Search-ADAccount -lockedout | Select-Object Name, SamAccountName
  •  Tags:  
  • Related