Skip to content

Instantly share code, notes, and snippets.

@poshcodebear
Created March 26, 2018 22:41
Show Gist options
  • Save poshcodebear/af5e74a3f6ea90475c2d67bcebabd02d to your computer and use it in GitHub Desktop.
Save poshcodebear/af5e74a3f6ea90475c2d67bcebabd02d to your computer and use it in GitHub Desktop.
New-RandomPassword
function New-RandomPassword {
$charset = ([char]'a'..[char]'z').foreach({[char]$_})
$RawPassword = ''
for ($i = 0; $i -lt 14; $i++)
{
# First and last 4 are letters
if ($i -lt 4 -or $i -gt 9)
{
if ($i % 2 -eq 0)
{ $set = $charset -match '[^aeiou]' }
else
{ $set = $charset -match '[aeiou]' }
}
else
{ $set = 0..9 }
$RawPassword += [string](Get-Random $set)
if ($i -eq 0)
{ $RawPassword = $RawPassword.ToUpper() }
}
$SecureString = ConvertTo-SecureString -AsPlainText $RawPassword -Force
[PSCustomObject]@{
'RawPassword' = $RawPassword
'SecureString' = $SecureString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment