Create Random Password with PowerShell

Create Random Password with PowerShell

Q: How can I create a random password using Windows PowerShell?

A: There is no simple cmdlet to create a random string in PowerShell. However there's a System.Random object which can be used to extract values in the range of 33-126 that are the characters from the ASCII table that are valid for Windows passwords. The code below creates a 12-character new password:

$randomObj = New-Object System.Random
$NewPassword=""
1..12 | ForEach { $NewPassword = $NewPassword + [char]$randomObj.next(33,126) }
$NewPassword

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish