Functions/New-RandomPassword.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
function New-RandomPassword { <# .SYNOPSIS Creates a new random password .DESCRIPTION Creates a new random password. Parameters can be passed to determine minimum and maximum password lengths, whether to avoid characters that are similar to one another or to limit it to readable words. Please read .NOTES section. .PARAMETER MinLength Integer representing minimum password length, valid range 8-102 characters. ParameterSetName: ReadableTitleCase, ReadableRandomCase, Web .PARAMETER MaxLength Integer representing maximum password length, valid range 8-102 characters. ParameterSetName: ReadableTitleCase, ReadableRandomCase, Web .PARAMETER NonAlphaChars Integer representing the number of non alphabetic characters. ParameterSetName: Web .PARAMETER Readable Switch indicating to use combinations of English words. Default behavior is to output a words that are title cased. ParameterSetName: ReadableTitleCase, ReadableRandomCase .PARAMETER AvoidSimilar Switch to prevent characters that are similar to one another to be included. For instance 1, l, I. ParameterSetName: ReadableTitleCase, ReadableRandomCase, Web .PARAMETER TitleCase Switch to capitalize each word. ParameterSetName: ReadableTitleCase .PARAMETER RandomCase Switch to randomly capitalize letters in each word. ParameterSetName: ReadableRandomCase .PARAMETER Web Switch to use web algorithm. ParameterSetName: Web .PARAMETER FullWordList Switch to use full word list of 370,103 words vs. 38,000 words .EXAMPLE New-RandomPassword -Web .EXAMPLE New-RandomPassword -MinLength 16 -AvoidSimilar .EXAMPLE New-RandomPassword -Readable -MinLength 8 -RandomCase .EXAMPLE New-RandomPassword -MinLength 16 -AvoidSimilar -Web .EXAMPLE New-RandomPassword -MinLength 16 -MaxLength 20 -Readable -AvoidSimilar .NOTES Changes: Added -TitleCase switch so that each word is capitalized Added -RandomCase switch so that each letter is randomly capitalized Added -Web switch so that web algorithm is used Changed parameter set names to more accurately reflect what they do Updated help comments DefaultParameterSetName is 'ReadableTitleCase' Added 'Q' to similar regex given closeness to 'O' #> #region parameter [CmdletBinding(DefaultParameterSetName = 'ReadableTitleCase', ConfirmImpact = 'None')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] [OutputType('string')] Param ( [Parameter(ParameterSetName = 'Web')] [Parameter(ParameterSetName = 'ReadableRandomCase')] [Parameter(ParameterSetName = 'ReadableTitleCase')] [ValidateRange(8, 102)] [int] $MinLength = 12, [Parameter(ParameterSetName = 'Web')] [Parameter(ParameterSetName = 'ReadableTitleCase')] [Parameter(ParameterSetName = 'ReadableRandomCase')] [ValidateRange(8, 102)] [int] $MaxLength, [Parameter(ParameterSetName = 'Web')] [int] $NonAlphaChars, [Parameter(ParameterSetName = 'ReadableTitleCase')] [Parameter(ParameterSetName = 'ReadableRandomCase')] [switch] $Readable, [Parameter(ParameterSetName = 'Web')] [Parameter(ParameterSetName = 'ReadableTitleCase')] [Parameter(ParameterSetName = 'ReadableRandomCase')] [switch] $AvoidSimilar, [Parameter(ParameterSetName = 'ReadableTitleCase')] [switch] $TitleCase, [Parameter(ParameterSetName = 'ReadableRandomCase')] [switch] $RandomCase, [Parameter(ParameterSetName = 'Web')] [switch] $Web, [Parameter(ParameterSetName = 'ReadableTitleCase')] [Parameter(ParameterSetName = 'ReadableRandomCase')] [switch] $FullWordList ) #endregion parameter begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" Write-Verbose -Message "ParameterSetName [$($PsCmdlet.ParameterSetName)]" $SimilarRegex = '\+|''|\-|0|1|I|O|Q|_|`|l|o|t|\|' Write-Verbose -Message "SimilarRegex = [$SimilarRegex]" if (-not $MaxLength) { $MaxLength = $MinLength } switch ($PsCmdlet.ParameterSetName) { 'Web' { } 'ReadableTitleCase' { $Symbol = Get-PrintableAscii -Verbose:$false | Where-Object { $_.Class -eq 's' } $MaxWords = [int] ($MaxLength / 3) $MinWords = [int] ($MinLength / 6) $Words = Get-WordList -Full:$FullWordList -Verbose:$false | Where-Object { $_.Length -ge 3 -and $_.Length -le 6 } $Sample = $Words | Get-Random -Count ($MinWords * 30) -Verbose:$false | Format-TitleCase -Verbose:$false } 'ReadableRandomCase' { $Symbol = Get-PrintableAscii -Verbose:$false | Where-Object { $_.Class -eq 's' } $MaxWords = [int] ($MaxLength / 3) $MinWords = [int] ($MinLength / 6) $Words = Get-WordList -Full:$FullWordList -Verbose:$false | Where-Object { $_.Length -ge 3 -and $_.Length -le 6 } $Sample = $Words | Get-Random -Count ($MinWords * 30) -Verbose:$false | Format-RandomCase -Verbose:$false } } } process { switch ($PsCmdlet.ParameterSetName) { 'Web' { if (-not $NonAlphaChars) { $NonAlphaChars = 0 } if ($MinLength -eq $MaxLength) { $Length = $MinLength } else { $Length = Get-Random -Minimum $MinLength -Maximum ($MaxLength + 1) } do { if (-not $AvoidSimilar) { $ReturnVal = [System.Web.Security.Membership]::GeneratePassword($Length, $nonAlphaChars) Write-Verbose -Message "PW is [$ReturnVal]" } else { do { Write-Verbose -Message 'in do' $ReturnVal = [System.Web.Security.Membership]::GeneratePassword($Length, $nonAlphaChars) Write-Verbose -Message "PW is [$ReturnVal]" } until (-not ($ReturnVal -cmatch $SimilarRegex)) } } until (($ReturnVal.Length -ge $MinLength) -and ($ReturnVal.Length -le $MaxLength)) $ReturnVal } 'ReadableTitleCase' { do { if (-not $AvoidSimilar) { $RandomSymbol = $Symbol.Char | Get-Random -Verbose:$false $RandomDigit = 0..9 | Get-Random -Verbose:$false $curWords = Get-Random -Minimum $MinWords -Maximum ($MaxWords + 1) -Verbose:$false Write-Verbose -Message "Symbol [$RandomSymbol] Digit [$RandomDigit]" $ReturnVal = ( @(($Sample | Get-Random -Count $curWords -Verbose:$false), $RandomDigit, $RandomSymbol) | Get-Random -Count ($curWords + 2)) -join '' } else { $RandomSymbol = $Symbol.Char | Where-Object { -not ($_ -cmatch $SimilarRegex) } | Get-Random -Verbose:$false $RandomDigit = 0..9 | Where-Object { -not ($_ -cmatch $SimilarRegex) } | Get-Random -Verbose:$false $curWords = Get-Random -Minimum $MinWords -Maximum ($MaxWords + 1) -Verbose:$false $Sample = $Sample | Where-Object { -not ($_ -cmatch $SimilarRegex) } $ReturnVal = ( @(($Sample | Get-Random -Count $curWords -Verbose:$false), $RandomDigit, $RandomSymbol) | Get-Random -Count ($curWords + 2)) -join '' } } until (($ReturnVal.Length -ge $MinLength) -and ($ReturnVal.Length -le $MaxLength)) $ReturnVal } 'ReadableRandomCase' { do { if (-not $AvoidSimilar) { $RandomSymbol = $Symbol.Char | Get-Random -Verbose:$false $RandomDigit = 0..9 | Get-Random -Verbose:$false $curWords = Get-Random -Minimum $MinWords -Maximum ($MaxWords + 1) -Verbose:$false Write-Verbose -Message "Symbol [$RandomSymbol] Digit [$RandomDigit]" $ReturnVal = ( @(($Sample | Get-Random -Count $curWords -Verbose:$false), $RandomDigit, $RandomSymbol) | Get-Random -Count ($curWords + 2)) -join '' } else { $RandomSymbol = $Symbol.Char | Where-Object { -not ($_ -cmatch $SimilarRegex) } | Get-Random -Verbose:$false $RandomDigit = 0..9 | Where-Object { -not ($_ -cmatch $SimilarRegex) } | Get-Random -Verbose:$false $curWords = Get-Random -Minimum $MinWords -Maximum ($MaxWords + 1) -Verbose:$false $Sample = $Sample | Where-Object { -not ($_ -cmatch $SimilarRegex) } $ReturnVal = ( @(($Sample | Get-Random -Count $curWords -Verbose:$false), $RandomDigit, $RandomSymbol) | Get-Random -Count ($curWords + 2)) -join '' } } until (($ReturnVal.Length -ge $MinLength) -and ($ReturnVal.Length -le $MaxLength)) $ReturnVal } } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |