How to Generate Random Password using PowerShell

Try the below powershell script to generate random password as per required characters and length. With the help of below scrip you can 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 }

Step by step Procedure to run the PowerShell script

Firstly, require powershell on OS, which is default available.

Then you have copy blue text content to notepad and save it as a .ps1 extenstion. Like passscript.ps1 and file is kept at c:\temp location so path will be c:\temp\passscript.ps1

After that open Powershell (run as administrator)

Then 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

Finally, run the powershell script which you have stored as a .ps1 with below command.


Follow above steps to generate different password.