Saturday, November 16, 2013

Generate random password - Powershell

I have written below script to generate random password with required characters and length. This script helps whenever I want to configure complex passwords to services or any account user.

function Generate-Password {
$alphabets= "abcdefghijklmnopqstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()"

$char = for ($i = 0; $i -lt $alphabets.length; $i++) { $alphabets[$i] }

for ($i = 1; $i -le 9; $i++)
Write-host -nonewline $(get-random $char)
if ($i -eq 9) { write-host `n }


1) To run this script you will require powershell on your OS, on windows 7 it is by default available.
2) Copy this yellow text content to notepad and save it as a .ps1 extenstion. (in my case file name is script.ps1 and file is kept at c:\temp location so path will be c:\temp\script.ps1)
3) Open Powershell (run as administrator)
4) You will need to change executionpolicy of powershell so script can execute. type below command to change it. You will require admin rights for the same.
               Set-executionpolicy unrestricted
Press y to confirm
5) Then run the script which you have stored as a .ps1 with below command. (Read step 2 carefully)
6) Every time you follow step 5 it will generate different password. 

Powershell AD password (unique) reset and send email

No comments: