KB10114 - PS: Create complex password


There are several scripts and commands available to create more or less secure passwords with Power Shell available on the web. I personally used the method with Get-Random for some while but those passwords are not really unique and the bigger problem is they sometimes do not match complexity requirements. So I decided to switch to the more secure method >> System.Web.Security.Membership::GeneratePassword

This script code can be used to securely generate passwords. There are several versions available and I think more will follow as I need them. The two ones already available are:

  • The Quick and dirty one
    This can be used to just copy and paste the code to Power Shell. The Shell will ask you for a password length and the display the new password directly.
  • The Function Create-ComplexPassword
    Can be called with the desired length of a password. It returns the password as a string


Tested on (Win7, WinSrv2008R2, WinSrv2012R2, Win10)

Script code:

# Quick + Dirty
# just copy / paste the line to Power Shell
Write-Host -ForegroundColor Yellow "Password length: " -NoNewline ;$Length = Read-Host ; $Assembly = Add-Type -AssemblyName System.Web;Write-Host -ForegroundColor Yellow "New password: " -NoNewline; Write-Host -ForegroundColor Green ([System.Web.Security.Membership]::GeneratePassword($Length,1))

# Better one 
# as function
function Create-ComplexPassword ($Length) {
    $Assembly = Add-Type -AssemblyName System.Web
    #Number of non alphanumeric characters that will be enforced (see https://msdn.microsoft.com/en-us/library/system.web.security.membership.generatepassword(v=vs.110).aspx)
    $numberOfNonAlphanumericCharacters = 1 
    return ([System.Web.Security.Membership]::GeneratePassword($Length,$numberOfNonAlphanumericCharacters))

>> syntax highlighting powered by highlight.js