Skip to content

Instantly share code, notes, and snippets.

@cdhunt
Created February 16, 2024 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdhunt/090cef766bd33764a4b6b084a2e56d4f to your computer and use it in GitHub Desktop.
Save cdhunt/090cef766bd33764a4b6b084a2e56d4f to your computer and use it in GitHub Desktop.
# Requires -Module NameIt, Humanizer
function New-Password {
[CmdletBinding(DefaultParameterSetName = 'Random')]
param (
[Parameter(ParameterSetName = 'Random')]
[switch]
$Random,
[Parameter(ParameterSetName = 'Random')]
[switch]
$Numbers,
[Parameter(ParameterSetName = 'Random')]
[switch]
$Symbols,
[Parameter(ParameterSetName = 'Memorable')]
[switch]
$Memorable,
[Parameter(ParameterSetName = 'Memorable')]
[switch]
$FullWord,
[Parameter(ParameterSetName = 'Memorable')]
[switch]
$Capitalize,
[Parameter(Position = 0)]
[ValidateRange(1, 128)]
[int]
$Length,
[Parameter()]
[switch]
$AsSecureString
)
if ($Memorable) {
switch ($Length) {
{ $_ -ge 5 } {
$Length = 5
$pattern = $FullWord ? '[verb]-[adjective]-[adjective]-[noun]-[noun]' : '[syllable][syllable]-[syllable][syllable]-[syllable][syllable]-[syllable][syllable]-[syllable][syllable]'
}
{ $_ -eq 0 -or $_ -eq 3 } {
$Length = 3
$pattern = $FullWord ? '[verb]-[adjective]-[noun]' : '[syllable][syllable]-[syllable][syllable]-[syllable][syllable]'
}
4 {
$pattern = $FullWord ? '[verb]-[adjective]-[adjective]-[noun]' : '[syllable][syllable]-[syllable][syllable]-[syllable][syllable]-[syllable][syllable]'
}
2 {
$pattern = $FullWord ? '[adjective]-[noun]' : '[syllable][syllable]-[syllable][syllable]'
}
1 {
$pattern = $FullWord ? '[noun]' : '[syllable][syllable]'
}
}
$string = Invoke-Generate -Template $pattern | ConvertTo-Casing -Case LowerCase
if ($Capitalize) {
$parts = $string.Split('-')
$change = Get-Random -Minimum 0 -Maximum ($parts.Count - 1)
$parts[$change] = $parts[$change].ToTitleCase()
$string = $parts -join '-'
}
} else {
$listAlphaLower = 'a'..'z'
$listAlphaUpper = 'A'..'Z'
$listNumber = 0..9
$listSymbol = @('!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '{', '}', '['. ']', ',', '.', '/', '?')
$charset = $listAlphaLower + $listAlphaUpper
if ($Numbers) { $charset += $listNumber }
if ($Symbols) { $charset += $listSymbol }
if ($Length -eq 0) { $Length = 16 }
$string = ($charset | Get-Random -Count $Length) -join ''
}
If ($AsSecureString) {
return $string | ConvertTo-SecureString -AsPlainText -Force
}
return $string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment