Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
Forked from dotps1/New-RandomPassword.ps1
Created October 6, 2017 19:01
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 midnightfreddie/41a60b0a07616f477db8aebfa7f76091 to your computer and use it in GitHub Desktop.
Save midnightfreddie/41a60b0a07616f477db8aebfa7f76091 to your computer and use it in GitHub Desktop.
Create a new random password with powershell, specify length, upper case, lower case, numbers, symbols. all are ASCII mapped arrays.
<#
.SYNOPSIS
Creates random password string of length 1 to 100.
.DESCRIPTION
Creates random password with ability to choose what characters are in the string and the length, the symbols can be specificlly defined.
.EXAMPLE
New-RandomPassword -Length 8 -Lowercase
In this example, a random string that consists of 8 lowercase charcters will be returned.
.EXAMPLE
New-RandomPassword -Length 15 -Lowercase -Uppercase -Numbers -Symbols
In this example, a random string that consists of 15 lowercase, uppercase, alpha-numeric and symbols will be returned.
.EXAMPLE
New-RandomPassword -Length 15 -Lowercase -Uppercase -Numbers -Symbols -Exclude ("1", "l", "0", "O", "o", "i", "I")
In this example, a random string that consists of 15 lowercase, uppercase, alpha-numeric and symbols will be returned
without "1", "l", "0", "O", "o", "i", or "I".
.LINK
http://www.asciitable.com/
https://gist.github.com/PowerShellSith
#>
function New-RandomPassword
{
[CmdletBinding()]
[OutputType([string])]
Param
(
# Length, Type uint32, Length of the random string to create.
[Parameter(Mandatory=$true,
Position=0)]
[ValidatePattern('[0-9]+')]
[ValidateRange(1,100)]
[uint32]
$Length,
# Array of characters to not include in password
[Parameter(Mandatory=$false)]
[char[]]
$Exclude,
# Lowercase, Type switch, Use lowercase characters.
[Parameter(Mandatory=$false)]
[switch]
$Lowercase=$false,
# Uppercase, Type switch, Use uppercase characters.
[Parameter(Mandatory=$false)]
[switch]
$Uppercase=$false,
# Numbers, Type switch, Use alphanumeric characters.
[Parameter(Mandatory=$false)]
[switch]
$Numbers=$false,
# Symbols, Type switch, Use symbol characters.
[Parameter(Mandatory=$false)]
[switch]$Symbols=$false
)
Begin
{
if (-not($Lowercase -or $Uppercase -or $Numbers -or $Symbols))
{
throw "You must specify one of: -Lowercase -Uppercase -Numbers -Symbols"
}
# Specifies bitmap values for character sets selected.
$CHARSET_LOWER=1
$CHARSET_UPPER=2
$CHARSET_NUMBER=4
$CHARSET_SYMBOL=8
# Creates character arrays for the different character classes, based on ASCII character values.
$ExcludeRegex = $Exclude -join "|"
$charsLower=97..122 | %{ [Char] $_ } | Where-Object { $PSItem -notmatch $ExcludeRegex }
$charsUpper=65..90 | %{ [Char] $_ } | Where-Object { $PSItem -notmatch $ExcludeRegex }
$charsNumber=48..57 | %{ [Char] $_ } | Where-Object { $PSItem -notmatch $ExcludeRegex }
$charsSymbol=35,36,40,41,42,44,45,46,47,58,59,63,64,92,95 | %{ [Char] $_ } | Where-Object { $PSItem -notmatch $ExcludeRegex }
}
Process
{
# Contains the array of characters to use.
$charList=@()
# Contains bitmap of the character sets selected.
$charSets=0
if ($Lowercase)
{
$charList+=$charsLower
$charSets=$charSets -bor $CHARSET_LOWER
}
if ($Uppercase)
{
$charList+=$charsUpper
$charSets=$charSets -bor $CHARSET_UPPER
}
if ($Numbers)
{
$charList+=$charsNumber
$charSets=$charSets -bor $CHARSET_NUMBER
}
if ($Symbols)
{
$charList+=$charsSymbol
$charSets=$charSets -bor $CHARSET_SYMBOL
}
<#
.SYNOPSIS
Test string for existnce specified character.
.DESCRIPTION
examins each character of a string to determine if it contains a specificed characters
.EXAMPLE
Test-StringContents i string
#>
function Test-StringContents([String] $test, [Char[]] $chars)
{
foreach ($char in $test.ToCharArray())
{
if ($chars -ccontains $char)
{
return $true
}
}
return $false
}
do
{
# No character classes matched yet.
$flags=0
$output=""
# Create output string containing random characters.
1..$Length | % { $output+=$charList[(get-random -maximum $charList.Length)] }
# Check if character classes match.
if ($Lowercase)
{
if (Test-StringContents $output $charsLower)
{
$flags=$flags -bor $CHARSET_LOWER
}
}
if ($Uppercase)
{
if (Test-StringContents $output $charsUpper)
{
$flags=$flags -bor $CHARSET_UPPER
}
}
if ($Numbers)
{
if (Test-StringContents $output $charsNumber)
{
$flags=$flags -bor $CHARSET_NUMBER
}
}
if ($Symbols)
{
if (Test-StringContents $output $charsSymbol)
{
$flags=$flags -bor $CHARSET_SYMBOL
}
}
}
until ($flags -eq $charSets)
}
End
{
$output
}
}
@midnightfreddie
Copy link
Author

In reply to this reddit question, I forked this gist and added the ability to exclude specific characters from the generated passwords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment