Created
June 27, 2014 13:43
-
-
Save kiidax/ef4bbe0ab5159262c52f to your computer and use it in GitHub Desktop.
Password generator by PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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