Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active August 29, 2015 14:26
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 JFFail/c233ac14e483ea273bb4 to your computer and use it in GitHub Desktop.
Save JFFail/c233ac14e483ea273bb4 to your computer and use it in GitHub Desktop.
PowerShell script to generate a random password of specified length and complexity.
#Stupid test script to generate a random, possibly complex password.
param
(
[Parameter(Mandatory=$true)]
[int]$Length,
[Parameter(Mandatory=$false)]
[switch]$Complexity
)
function MakePassword
{
param
(
$IsComplex,
$Length
)
#Define the array of possible options.
#Note that index 61 is the cut-off for non-complex passwords!
$characterArray = @("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", `
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", `
"z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", `
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", `
"2", "3", "4", "5", "6", "7", "8", "9", "0", "~", "!", "@", "#", "$", `
"%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "[", "]", "{", "}", `
"|", "\", ":", ";", "<", ",", ">", ".", "?", "/")
#Create an array to house the final password value.
$passwordValue = ""
$counter = 0
if($IsComplex) {
$ceiling = $characterArray.Length
} else {
$ceiling = 61
}
#Loop through and pick a character.
for($i = 0; $i -lt $Length; $i++)
{
#Make a random number.
$index = Get-Random -Minimum 0 -Maximum $ceiling
$currentValue = $characterArray[$index]
#Put that into the array.
$passwordValue += $currentValue
}
#Write the password to the screen.
Write-Output $passwordValue -
}
#Call the function.
MakePassword -IsComplex $Complexity -Length $Length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment