Skip to content

Instantly share code, notes, and snippets.

@jpbruckler
Last active October 30, 2018 14:54
Show Gist options
  • Save jpbruckler/b1f46bb892fcc23fad603d104e90416a to your computer and use it in GitHub Desktop.
Save jpbruckler/b1f46bb892fcc23fad603d104e90416a to your computer and use it in GitHub Desktop.
Generates a random password
function New-RandomPassword {
<#
.SYNOPSIS
Generates a random password.
.DESCRIPTION
Generates a random password that is $Length characters long and
contains a minimum of $MinSpecial special characters.
.PARAMETER Lenth
The character length of the password to generate. Length given must
be between 8 and 100. Default is 25.
.PARAMETER MinSpecial
The _minimum_ number of special characters that the password should
contain. Default is 7.
.PARAMETER Count
The number of passwords to generate. Default is 1.
.EXAMPLE
PS C:\> New-RandomPassword
i*^4P0_|aj&e$}QYNUgs4gq4h
The example above generates a single, 25 character random password with
a minimum of 7 special characters.
.EXAMPLE
PS C:\> New-RandomPassword 12 4 5
iW9#+L^PuDr%
?!doG*lE_0xM
3!IoNU:xNc]>
i!g/8)933ht>
;!bmj_d{tKKA
The example above generates 5 random passwords, with a character length
of 12, and a minimum special character count of 4.
#>
param(
[Parameter( Position = 0)]
[ValidateRange(8,100)]
[Alias('l')]
[int] $Length = 25,
[Parameter( Position = 1)]
[Alias('s')]
[int] $MinSpecial = 7,
[Parameter( Position = 2)]
[ValidateScript({ $_ -gt 0 })]
[Alias('c')]
[int] $Count = 1
)
begin {
$null = [Reflection.Assembly]::LoadWithPartialName('system.web')
}
process {
for ($i=1;$i -le $Count; $i++) {
Write-Output $([System.Web.Security.Membership]::GeneratePassword($Length,$MinSpecial))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment