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 [Reflection.Assembly]::LoadWithPartialName("System.Web") #Calling GeneratePassword Method [System.Web.Security.Membership]::GeneratePassword(10,0)

Regards
Sherif Talaat (Twitter: @Sheriftalaat)