Enable and set passwords for user accounts on bulk
Howdy Sloths, this week we present an easy solution for a time consuming task. Imagine you had 200 user accounts that had been created and disabled. During the creation passwords weren’t recorded so when we enable the accounts – none of the accounts will be usable. Thankfully Powershell makes this task a breeze!
First up, we need to have an input to feed our command. We used a pretty simple CSV that contained two fields – the username and the password we had generated for the user. Here is an example:
Username,Passwd CoolUser,HorsePasswordBarn57074 SlightyCoolerUser,PonyPasswordShed3210
Ideally you would generate great passwords – try StrongPasswordGenerator. Once you have populated your input file with your huge amount of users we are good to execute our code.
# Import required for old versions of PS
Import-Module ActiveDirectory
# Lets grab the file we created above
$colUser = Import-Csv UserData.csv
# Here we loop through each user in our input file
Foreach ($objUser in $colUser) {
# Set up an error catch flag
$ErrorFlag = $false
Try {
# First up, lets set the password to the one we generated and stored above.
Set-ADAccountPassword -Identity $objUser.Username -NewPassword (ConvertTo-SecureString -AsPlainText $objUser.Passwd -Force)
} Catch {
# Catch any errors
write-host "Error: $_"
$ErrorFlag = $true
}
# Ensure we don't have an error before we enable the user
If (!$ErrorFlag) {
# Then we want to enable the account
Enable-ADAccount $objUser.Username
write-host "$($objUser.Username) enabled and password set."
}
}
And there we have it – how to enable and set passwords for users on bulk.
