One-Liner: Generate a Random Password

Always learning. Forever delighted. Happily surprised.

That's using PowerShell for you... well, me. 

The other week one of my customers showed me this little beauty:



The GeneratePassword static method accepts two values: the first parameter is the number of characters in the generated password; the second parameter is the number of non-alphanumeric characters to include.

With the above one-liner we get a 20 character password with 5 non-alphanumeric characters. For example:


This has replaced the lengthy New-Password function I had in my $profile...


Comments (4)

  1. David says:

    I'm missing something, running PS v4.0 but…

    Unable to find type [System.Web.Security.Membership]. Make sure that the assembly that contains this type is loaded.

    Clue pls!

  2. Dave_McI5 says:

    Need to add this line in 1st Add-Type -AssemblyName System.web
    if your feeling lucky add it all on one line Add-Type -AssemblyName System.web;[System.Web.Security.Membership]::GeneratePassword(20,5)

  3. Ed says:

    Thank you, this was very useful.

Skip to main content