Skip to content

Instantly share code, notes, and snippets.

@kiidax
Created June 27, 2014 13:43
Show Gist options
  • Save kiidax/ef4bbe0ab5159262c52f to your computer and use it in GitHub Desktop.
Save kiidax/ef4bbe0ab5159262c52f to your computer and use it in GitHub Desktop.
Password generator by PowerShell
param([switch]$Upper, [switch]$Lower, [switch]$Number, [int]$Length = 8)
function Make-CharArray($FirstChar, $LastChar)
{
$FirstIndex = [char]::ConvertToUtf32($FirstChar, 0)
$LastIndex = [char]::ConvertToUtf32($LastChar, 0)
$Chars = $FirstIndex..$LastIndex | ForEach-Object { [char]::ConvertFromUtf32($_) }
return $Chars
}
$UpperChars = Make-CharArray 'A' 'Z'
$LowerChars = Make-CharArray 'a' 'z'
$NumberChars = Make-CharArray '0' '9'
if (!($Upper -or $Lower -or $Number))
{
$Upper = $Lower = $Number = $True
}
$Chars = @()
if ($Upper) { $Chars = $Chars + $UpperChars }
if ($Lower) { $Chars = $Chars + $LowerChars }
if ($Number) { $Chars = $Chars + $NumberChars }
$Random = New-Object Random
[string]::Concat((1..$Length | ForEach-Object { $Chars[$Random.Next(0, $Chars.Length - 1)] }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment