Windows PowerShell 4.0 Examples – How to Generate Complex Password

Last Updated on

There is no built-in cmdlet to generate a password, but we can leverage a vast number of .NET Framework classes. System.Web.Security.Membership class has a static method GeneratePassword(). First, we need to load an assembly containing this class, and then we use GeneratePassword() to define password length and a number of non-alphanumeric characters.

1
2
$Assembly = Add-Type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(8,3)

How do we know all that? With a little help from the Get-Member cmdlet (and simplified where syntax):

1
[System.Web.Security.Membership] | Get-Member -MemberType method -Static| where name -match password
Lima

About the author

Lima is the visual nautical indicator for "stop instantly."

Leave a Reply

Welcome!

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.