In this article we will show you how to generate random password using Powershell

We will use the GeneratePassword method of the System.Web.Security.Membership class.

Syntax

Public Shared Function GeneratePassword (length As Integer, numberOfNonAlphanumericCharacters As Integer) As String

Parameters

length

The number of characters in the generated password. The length must be between 1 and 128 characters.

numberOfNonAlphanumericCharacters

The minimum number of non-alphanumeric characters (such as @, #, !, %, &, and so on) in the generated password.

Examples

Lets see some examples of this

The first is 8 characters in length with 3 non-alphanumeric characters I need.

 

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

This is the output that I saw

powershell password

powershell password

In this example we will generate a 16 characters in length with 8 alphanumeric characters as well

 

Add-Type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(16,8)

This will generate 10 passwords of 16 characters in length with 8 alphanumeric characters as well

 

Add-Type -AssemblyName System.Web
1..10 | % { [System.Web.Security.Membership]::GeneratePassword(16,8) }

I saw these generated

_sy.p1-.m+RW_d_{
_hN.mc}_Q-E}_n_9
nY1.6(!&iw&%e}_v
4H(.$B!Cz%^E3Y_:
{k&.qF!@g0P=_J_g
3&o.OE!aGZ5._%_-
{&h.Gd!4EP-X_8_-
Qfz(=X.$$MBu*^$j
_4_.0={iQ.e+CQ_j
/@D.if;_Hp]qK}_O

 

If you want to create a file with all of the generated password you can use the following

 

Add-Type -AssemblyName System.Web
1..10 | % { [System.Web.Security.Membership]::GeneratePassword(16,8) } | Out-File .\Passwords.txt

Video