Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created September 24, 2015 18:05
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 gravejester/599a9cd02e7f5d19f8c7 to your computer and use it in GitHub Desktop.
Save gravejester/599a9cd02e7f5d19f8c7 to your computer and use it in GitHub Desktop.
function Test-PasswordStrength {
<#
.SYNOPSIS
Test password strength.
.DESCRIPTION
This functions lets input a password to test it strength.
.EXAMPLE
Test-PasswordStrength 'MyCoolPassword'
.NOTES
Based on code example from https://social.msdn.microsoft.com/Forums/vstudio/en-US/5e3f27d2-49af-410a-85a2-3c47e3f77fb1/how-to-check-for-password-strength?forum=csharpgeneral
Author: Øyvind Kallstad
Date: 24.09.2015
Version: 1.0
.OUTPUTS
System.String
.LINK
https://communary.wordpress.com/
#>
[CmdletBinding()]
param (
# The password to test
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string] $Password
)
$score = 0
if ($Password.Length -lt 5) {
Write-Verbose 'Password length is less than 5 characters.'
Write-Output 'Very Weak'
break
}
if ($Password.Length -ge 8) {
Write-Verbose 'Password length is equal to or greater than 8 characters. Added 1 to password score.'
$score++
}
if ($Password.Length -ge 12) {
Write-Verbose 'Password length is equal to or greater than 12 characters. Added 1 to password score.'
$score++
}
if ([regex]::IsMatch($Password, '\d+')) {
Write-Verbose 'Password contains numbers. Added 1 to password score.'
$score++
}
if ([regex]::IsMatch($Password, '[a-z]+')) {
Write-Verbose 'Password lowercase letters. Added 1 to password score.'
$score++
}
if ([regex]::IsMatch($Password, '[A-Z]+')) {
Write-Verbose 'Password uppercase letters. Added 1 to password score.'
$score++
}
if ([regex]::IsMatch($Password, '[!@#$%^&*?_~-£(){},]+')) {
Write-Verbose 'Password symbols. Added 1 to password score.'
$score++
}
switch ($score) {
1 { $passwordScore = 'Very Weak' }
2 { $passwordScore = 'Weak' }
3 { $passwordScore = 'Medium' }
4 { $passwordScore = 'Strong' }
5 { $passwordScore = 'Strong' }
6 { $passwordScore = 'Very Strong' }
}
Write-Output $passwordScore
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment