#This is the file that will be generated with the users account ID and the password generated.
[String]$path= ".\NewPass.txt"
#This will check if the file exist and will delete that file so a new one can be created from the scratch
#If the doesn't exist will through an error saying that the file doesn't exist and will continue.
if ($path -ne $null){Remove-Item $path}
<# Required Assembly to Generate Passwords #>
Add-Type -Assembly System.Web
#In my case I created a OU for test purposes here it is.
#You need to change it to meet your requirements.
$OU="OU=Users,OU=Test,OU=SATESTENV,DC=SATESTENV,DC=local"
#Get the users inside the OU specified in the Options Above
$users=Get-ADUser -filter * -SearchBase $OU
foreach($Name in $users.samaccountname){
#Variable that will receive the random password
$NewPassword=[Web.Security.Membership]::GeneratePassword(8,3)
#The code below will change the password and will set the Option to change the password on the next logon.
Set-ADAccountPassword -Identity $Name -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $NewPassword -Force)
Get-ADUser -Identity $Name |Set-ADUser -ChangePasswordAtLogon:$true
#Here will write the info to the file, so you can communicate to your users the new password.
Write-Output "UserID:$name `t Password:$NewPassword" `n`n|FT -AutoSize >>NewPass.txt
}
Now we just need to test the Script, open your PowerShell and write the name of your PowerShell file followed by [ENTER], in my case is Random.ps1 and wait until the execution finished. This could take some time, depends of the number of users inside the Organizational Unit (OU).
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (4)
Commented:
Commented:
this is very helpful , but the generated password are very heavy ... how can i make the generated passwords alphanumeric with only lowercase and numbers ? and export it to a csv file ?
thanks
Author
Commented:Thank you all for your comments.
@robert for you to be able to do that with the code above you will need to use regexp, but I believe you can achieve it in a more easy way, check the example below.
In my code I have:
Open in new window
If you change it to:
Open in new window
This will generate a random char only with lower cases.
If you also want to capital letters you need use the ASCII table to check the value of each char, lets see an example.
Open in new window
The output will be a random string.
Now if you want to add numbers, you can do it by adding the ASCII value related with numbers. Lets check it.
Open in new window
I believe this can do the trick, I didn't had to much time to tested it, but I can see that some random passwords could only have letters.
Then you can export it to a csv file piping the variable $finalpass
Open in new window
With this little code you already have the tools to start tweaking and accomplish your goal.
I hope it helps.
Cheers.
D.
Commented:
i have another question i hope im not being annoying ... the exported csv shows like
your help is much appreciated ...