How to Generate a Secure Random Password using PowerShell

The goal of this post is to show you how you can use Windows PowerShell to call .NET Method().

Simply, all you have to know is the namespace, assembly name, and the method name.

So, how can I do that ?!

  • Load the required assembly in your PowerShell console using [Reflection.Assembly]::LoadWithPartialName("AssemblyName")
  • Call you Method [namespace]::Method(Parameters)

In the following example I'll use Membership.GeneratePassword  Method to generate a Complex Passwords randomly

# namespace: System.Web.Security
# assembly: System.Web (in System.Web.dll)
# method: GeneratePassword(int length, int numberOfNonAlphanumericCharacters)

#Load "System.Web" assembly in PowerShell console

#Calling GeneratePassword Method

Sherif Talaat (Twitter: @Sheriftalaat)

Comments (5)

  1. OldKarlm says:

    That's very nice!

    I like to have a selection uf passwords, so I do this:

    1..10 |%{[System.Web.Security.Membership]::GeneratePassword(10,0)}


  2. Anonymous says:

    NOTE: To stop randomly falling foul of the strong passwords law use a positive integer in position number 2 of the function call. For example: 16,2 .

  3. Meshack says:

    This is great, How can I assign that exact generated password? i.e $AccountPass=[Random Password here]

  4. microdot187 says:

    $AccountPass = $([System.Web.Security.Membership]::GeneratePassword(16,8))

  5. Dalmiro says:

    Worked for me 🙂

Skip to main content