Generate Random Password with Powershell

Posted Saturday, January 14, 2012 in Old JamesCMS Posts

Here's a quick powershell function to generate a random password. The idea here is to pick a random character from a char array. The length is also variable to decrease the collision domain.

function GeneratePassword ($length) {
     if (-not $length -gt 0) {$length = Get-Random -min 14 max 17}
     $chars = [char[]]’`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|\/<>,.;:01234567890123456789’
     $chars = $chars -join (1..$length|%{Get-Random $chars -count 1})