Skip to content

Instantly share code, notes, and snippets.

@josheinstein
Created July 19, 2012 17:40
Show Gist options
  • Save josheinstein/3145560 to your computer and use it in GitHub Desktop.
Save josheinstein/3145560 to your computer and use it in GitHub Desktop.
Simple PowerShell password generator
[CmdletBinding(DefaultParameterSetName='Normal')]
param(
[Parameter()]
[Int32]$Length = 8,
[Parameter(ParameterSetName="Complex")]
[Switch]$Complex,
[Parameter(ParameterSetName="Easy")]
[Switch]$Easy
)
begin {
$Upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$Lower = 'abcdefghijklmnopqrstuvwxyz'
$Digits = '01234567890'
$Special = '~!@#$%^&*()-_+=/\.'
$RandomGen = New-Object System.Random
}
process {
$Password = ''
switch ($PSCmdlet.ParameterSetName) {
'Easy' {
$Password += $Upper[$RandomGen.Next($Upper.Length)]
while ($Password.Length -lt ($Length / 2)) {
$Password += $Lower[$RandomGen.Next($Lower.Length)]
}
while ($Password.Length -lt $Length) {
$Password += $Digits[$RandomGen.Next($Digits.Length)]
}
}
'Normal' {
$CharSet = "$Upper$Lower$Digits$Digits$Digits"
while ($Password.Length -lt $Length) {
$Password += $CharSet[$RandomGen.Next($CharSet.Length)]
}
}
'Complex' {
$CharSet = "$Upper$Lower$Digits$Digits$Digits$Special$Special$Special"
while ($Password.Length -lt $Length) {
$Password += $CharSet[$RandomGen.Next($CharSet.Length)]
}
}
}
Return $Password
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment