Password: Generation
PowerShell for generating a random password.
function New-Password {
[CmdletBinding()]
[OutputType([string[]])]
param
()
process {
$uCharSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$lCharSet = 'abcdefghijklmnopqrstuvwxyz'
$nCharSet = '0123456789'
$sCharSet = '/*-+,!?=()@;:._'
# A minimum and maximum
$upperCaseMaximum = 3
$upperCaseMinimum = 2
$lowerCaseMaximum = 3
$lowerCaseMinimum = 2
$numberMaximum = 3
$numberMinimum = 2
$specialMaximum = 3
$specialMinimum = 1
# The default settings create a minimum possible length of 7 and a maximum of 12.
$randCountUpper = (Get-Random -Maximum $upperCaseMaximum) + $upperCaseMinimum
$upperCase = ($uCharSet.ToCharArray() `
| Get-Random -Count $randCountUpper | ForEach-Object { [char]$_ }) -join ''
$randCountLower = (Get-Random -Maximum $lowerCaseMaximum) + $lowerCaseMinimum
$lowercase = ($lCharSet.ToCharArray() `
| Get-Random -Count $randCountLower | ForEach-Object { [char]$_ }) -join ''
$randCountNumber = (Get-Random -Maximum $numberMaximum) + $numberMinimum
$number = ($nCharSet.ToCharArray() `
| Get-Random -Count $randCountNumber | ForEach-Object { [char]$_ }) -join ''
$randCountSpecial = (Get-Random -Maximum $specialMaximum) + $specialMinimum
$special = ($sCharSet.ToCharArray() `
| Get-Random -Count $randCountSpecial | ForEach-Object { [char]$_ }) -join ''
$inputChars = $upperCase + $lowercase + $number + $special
$password = ($inputChars.ToCharArray() `
| Get-Random -Count $inputChars.Length | ForEach-Object { [char]$_ }) -join ''
$password
}
}
New-Password