Last active
August 29, 2015 14:26
-
-
Save JFFail/c233ac14e483ea273bb4 to your computer and use it in GitHub Desktop.
PowerShell script to generate a random password of specified length and complexity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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