Skip to content

Instantly share code, notes, and snippets.

@JeremyTBradshaw
Last active April 13, 2023 01:02
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 JeremyTBradshaw/435c0dfdc085c14e2c7ab2fba7fa6766 to your computer and use it in GitHub Desktop.
Save JeremyTBradshaw/435c0dfdc085c14e2c7ab2fba7fa6766 to your computer and use it in GitHub Desktop.
PowerShell Password Generator
<#
.Synopsis
Generate one or more random passwords, from 7 to 64 characters in length.
.Description
Passwords are generated using an equal distribution of upper and lower case letters, numbers, space, and special
characters found on the number row.
.Parameter Length
Specifies the length of the generated password(s).
- Default: 16
- Minimum: 7
- Maximum 64
.Parameter Count
Specifies how many passwords to generate.
- Default: 1
- Miniumum: 1
- Maximum: Int32 max value
#>
param(
[ValidateRange(7, 64)]
[int]$Length = 16,
[ValidateRange(1, [int32]::MaxValue)]
[int]$Count = 1
)
foreach ($pw in (1..$Count)) {
$Lowercase = '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'
$Uppercase = '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'
$Numbers = '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
$SpecialCharacters = ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+'
$PasswordArray = @()
$TempLength = $Length
while ($TempLength % 4 -ne 0) { $TempLength++ }
do { $PasswordArray += Get-Random $Uppercase }
until ($PasswordArray.count -eq $TempLength / 4)
do { $PasswordArray += Get-Random $Lowercase }
until ($PasswordArray.count -eq ($TempLength / 4) * 2)
do { $PasswordArray += Get-Random $Numbers }
until ($PasswordArray.count -eq ($TempLength / 4) * 3)
do { $PasswordArray += Get-Random $SpecialCharacters }
until ($PasswordArray.count -eq ($TempLength / 4) * 4)
$Password = (Get-Random $PasswordArray -Count $Length) -join ''
Write-Output -InputObject "$($Password)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment