# encoding: ascii
# api: powershell
# title: New-RandomPassword
# description: Generates a random password of a specified length.
# version: 0.1
# type: function
# author: BattleChicken
# license: CC0
# function: New-RandomPassword
# x-poshcode-id: 6273
# x-derived-from-id: 6274
# x-archived: 2016-04-01T22:15:29
# x-published: 2016-03-30T13:52:00
#
#
Function New-RandomPassword {
Param(
[parameter()]
[ValidateRange(8,100)]
[int]$Length=12,
[parameter()]
[switch]$SpecialChars
)
$aUpper = [char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$aLower = [char[]]'abcdefghijklmnopqrstuvwxyz'
$aNumber = [char[]]'1234567890'
$aSCharacter = [char[]]'!@#$%^&*()_+|~-=\{}[]:;<>,./' # Depending on usage, you may want to strip out some of the special characters if they are not supported
$aClasses = [char[]]'ULN' #represents the different classes of characters
$thePass = ""
if ($SpecialChars){
$aClasses += 'S'
}
$notOneOfEach = $true
while ($notOneOfEach){
$pPattern = ''
for ($i=0; $i-lt $Length; $i++){
$pPattern += get-random -InputObject $aClasses
}
if (($pPattern -match 'U' -and $pPattern -match 'L' -and $pPattern -match 'N' -and (-not $specialChars)) -or ($pPattern -match 'U' -and $pPattern -match 'L' -and $pPattern -match 'N' -and $pPattern -match 'S')){
$notOneOfEach = $false
}
}
foreach ($chr in [char[]]$pPattern){
switch ($chr){
U {$thePass += get-random -InputObject $aUpper; break}
L {$thePass += get-random -InputObject $aLower; break}
N {$thePass += get-random -InputObject $aNumber; break}
S {$thePass += get-random -InputObject $aSCharacter; break}
}
}
$thePass
}