Skip to content

Instantly share code, notes, and snippets.

@xoner
Last active October 12, 2015 06:48
Show Gist options
  • Save xoner/3986820 to your computer and use it in GitHub Desktop.
Save xoner/3986820 to your computer and use it in GitHub Desktop.
Generate passwords in powershell throuht System.Web.Security.Membership
# You must load the System.Web assembly to your powershell session to make this script work.
[Reflection.Assembly]::LoadWithPartialName('System.Web')
function GenPassword
{
Param(
[int] $PasswordLength = 0,
[ValidateSet("low", "medium", "high")]
[string] $PasswordComplexity = "medium"
)
$lPasswordLength = 0
$lNonAlphanumeric = 0
switch ($PasswordComplexity)
{
"low"
{
if($PasswordLength -gt 0)
{
$lPasswordLength = $PasswordLength;
}
else
{
$lPasswordLength = 8
}
$lNonAlphanumeric = 0
}
"medium"
{
if ($PasswordLength -ge 2)
{
$lPasswordLength = $PasswordLength;
}
else
{
$lPasswordLength = 16
}
$lNonAlphanumeric = 2
}
"high"
{
if($PasswordLength -ge 4)
{
$lPasswordLength = $PasswordLength
}
else
{
$lPasswordLength = 32
}
$lNonAlphanumeric = 4
}
}
[System.Web.Security.Membership]::GeneratePassword(16,2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment