≡ Menu

PowerShell: Simple way to generate random passwords

In this post we will see the simplest way I came across to generate random passwords using PowerShell. There are other methods available with large features set but this one is good enough for most requirements

First let us look at the simplest way. The GeneratePassword method of System.Web.Security.Membership class simplifies the password generation.

Add-Type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(8,3)
Generate random password

As you can see from the above screen, these 2 simple lines of code generated a random password of length 8.

The GeneratePassword() method takes 2 arguments. First one is length of password and second one is number of non-alphanumerics you want in the random password. In my example, I have provided 8 as password length and 3 as number of non-alphanumeric characters I need.

You can use this approach to generate as many number of random passwords you want. Look at the below example.

1..10 | % { [System.Web.Security.Membership]::GeneratePassword(8,3) }
Generate multiple random passwords

There are other ways available as well. I am sharing some of the links that I came across.

  1. https://activedirectoryfaq.com/2017/08/creating-individual-random-passwords/
  2. https://gallery.technet.microsoft.com/scriptcenter/Password-Generator-using-0f99f008
  3. https://4sysops.com/archives/generate-complex-passwords-with-powershell/
  4. https://gist.github.com/indented-automation/2093bd088d59b362ec2a5b81a14ba84e

Hope this article helps. Let me know if you come across any easy and better way.

Comments on this entry are closed.

  • Mike March 30, 2019, 3:40 am

    FYI, if you hadn’t noticed, it seems your SSL cert for this site expired a few days ago (as of 3/25/19). You may want to look into LetsEncrypt and the Certbot feature to avoid this in the future.

  • nihha September 15, 2019, 6:53 am

    bruh nigga