Random Password Function

This started as a one liner PowerShell script that I threw together to generate a random password. It evolved into a small function. I attempted to make it seemingly as random as possible. I know because of the way Get-Random works, basing off the clock, that its not as “random” as it can be.

However I think I added enough layers of of randomness that it improved things as much as possible. This is probably more then sufficient for any password needs one would have.

It doesn’t include special characters but you could easily add those with another [char] array block of code to include those ascii elements.

function Get-JLRDRandomPassword{            

param (            
   [int]$Length = 32,            
   [int]$CharRepeatMax = 3,            
   [int]$Generate = 1            

   For($i=0; $i -lt $Generate; $i++)             
      ## * Randomly generate up to $CharRepeatMax sets of 0-9, A-Z, and a-z            
      ## * Randomly generate a password to $Length            
      ## * Ouput as many password objects as $Generate            
      $GeneratedPass = $(             

         $([char[]]@(48..57) * (Get-Random -Min 1 -Max ($CharRepeatMax + 1)) |             
            Get-Random -Count ([int]::MaxValue)) +             

         $([char[]]@(65..90) * (Get-Random -Min 1 -Max ($CharRepeatMax + 1)) |             
            Get-Random -Count ([int]::MaxValue)) +             

         $([char[]]@(97..122) * (Get-Random -Min 1 -Max ($CharRepeatMax + 1)) |             
            Get-Random -Count([int]::MaxValue)             

      ) | Get-Random -Count $Length) -join ""             

      New-Object PSObject -Property @{ Password = $GeneratedPass }            

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s