Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function Create-Password {
<#
.SYNOPSIS
Create-Password creates a random password
.DESCRIPTION
This function is used to create random passwords. You can specify what
character types (lowercase letters, uppercase letters, numbers and symbols)
as well as the minimum amount of those character types.
.PARAMETER Minimum
Specifies a minimum length of the password. If the value is less than the
combined total of Uppercase, Lowercase, Numbers and Symbols, then the
minimum password length will be that of the combined total.
Default 11
.PARAMETER Maximum
Specifies a maximum length of the password. If the value is less than the
combined total of Uppercase, Lowercase, Numbers and Symbols, then the
minimum password length will be that of the combined total. Also, if the
value is less than the 'Minimum' password length, 'Maximum' will be set to
the same value as 'Minimum'.
Default 15
.PARAMETER Uppercase
Specifies the minimum number of UPPERCASE letters in the password.
Set this value to -1 to disallow uppercase letters.
Default 1
.PARAMETER Lowercase
Specifies the minimum number of lowercase letters in the password.
Set this value to -1 to disallow lowercase letters.
Default 1
.PARAMETER Numbers
Specifies the minimum number of numbers in the password.
Set this value to -1 to disallow numbers.
Default 1
.PARAMETER Symbols
Specifies the minimum number of symbols in the password.
Set this value to -1 to disallow symbols.
Default 1
.EXAMPLE
Get-Password
8w4oZ&4zb3v8
.EXAMPLE
Get-Password -Minimum 8 -Maximum 8 -Uppercase 1 -Lowercase 1 -Numbers 1 -Symbols -1
ef9TtwLI
Creates a random password with exactly 8 characters. At least 1 UPPERCASE,
at least 1 lowercase, at least 1 number and NO symbols.
#>
Param(
[int]$Minimum = 11,
[int]$Maximum = 15,
[int]$Uppercase = 1,
[int]$Lowercase = 1,
[int]$Numbers = 1,
[int]$Symbols = 1
)
[int]$intLength = $Minimum
[int]$intMisc = 0
[int]$intTotal = $Uppercase + $Lowercase + $Numbers + $Symbols
If ($intTotal -gt $Minimum) {
$Minimum = $intTotal
}
If ($Minimum -gt $Maximum) {
$Maximum = $Minimum
}
If ($Maximum -gt $Minimum) {
$intLength = ($Minimum + (Get-Random -Minimum 0 -Maximum ($Maximum - $Minimum)))
} Else {
$intLength = $Maximum
}
$intMisc = ($intLength - $intTotal)
$arrUppercase = @('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')
$arrLowercase = @('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')
$arrNumbers = @(1,2,3,4,5,6,7,8,9,0)
$arrSymbols = @('!','@','#','%','&','(',')','.','+','=','-','_')
$arrCombined = @()
If ($Lowercase -ge 0) {
$arrCombined += $arrLowercase
}
If ($Uppercase -ge 0) {
$arrCombined += $arrUppercase
}
If ($Numbers -ge 0) {
$arrCombined += $arrNumbers
}
If ($Symbols -ge 0) {
$arrCombined += $arrSymbols
}
$arrPassword = @()
While ($Uppercase -gt 0) {
$arrPassword += Get-Random -InputObject $arrUppercase
$Uppercase = $Uppercase - 1
}
While ($Lowercase -gt 0) {
$arrPassword += Get-Random -InputObject $arrLowercase
$Lowercase = $Lowercase - 1
}
While ($Numbers -gt 0) {
$arrPassword += Get-Random -InputObject $arrNumbers
$Numbers = $Numbers - 1
}
While ($Symbols -gt 0) {
$arrPassword += Get-Random -InputObject $arrSymbols
$Symbols = $Symbols - 1
}
While ($intMisc -gt 0) {
$arrPassword += Get-Random -InputObject $arrCombined
$intMisc = $intMisc - 1
}
Get-Random -InputObject $arrPassword -Count $intLength | ForEach-Object { [string]$strPassword += $_ }
Return $strPassword
}
Create-Password -Minimum 15 -Maximum 15 -Uppercase 1 -Lowercase 1 -Numbers 1 -Symbols -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment