Last active
May 2, 2017 09:42
-
-
Save Sarafian/2ce5d18d33bf98ae758f2b6db9a59823 to your computer and use it in GitHub Desktop.
Generate new random password with 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
function New-RandomPassword | |
{ | |
param( | |
[Parameter(Mandatory=$false)] | |
[int]$Length=8 | |
) | |
begin { | |
} | |
process { | |
$numericChars=@( | |
'0123456789'.ToCharArray() | |
) | |
$upperCaseChars=@( | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.ToCharArray() | |
) | |
$lowerCaseChars=@( | |
'abcdefghijklmnopqrstuvwxyz'.ToCharArray() | |
) | |
$specialChars=@( | |
"{|}/>(<&*,!@;+#[]\~.):?%$'`"".ToCharArray() | |
) | |
$allChars=@( | |
$numericChars | |
$upperCaseChars | |
$lowerCaseChars | |
$specialChars | |
) | |
# Make sure one character of each group will exist in the password | |
$randomChars=@( | |
$numericChars | Get-Random | |
$upperCaseChars | Get-Random | |
$lowerCaseChars | Get-Random | |
$specialChars | Get-Random | |
) | |
# Add random character from all groups | |
$randomChars+=$allChars | Get-Random -Count ($Length - $randomChars.Length) | |
# Randomize and stringify | |
(-join ($randomChars | Get-Random -Count $randomChars.Length)).ToString() | |
} | |
end { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example 1
Example 2