Generate random password using ‘System.Web.Security’ namespace

I was recently required to compile a script which was required to generate a random password of a specified length and to match the default ‘Password must meet complexity requirements’ when the security setting is enabled.

This was quite simple to achieve (only required two lines) loading the ‘System.Web.Security’ namespace within Windows Powershell. Basically in order to generate a password with a required length you will need to modify the first parameter, in the below example I have chosen to¬†generate¬†a password of ’12’ characters in length (maximum length is ‘128’ characters).

Secondly, you will need to specify the¬†minimum number of non-alphanumeric characters in the generated password which I have configured to be ‘1’.

[Reflection.Assembly]::LoadWithPartialName(‚ÄúSystem.Web‚ÄĚ) > $null

The below has been taken from to describe the generate password method:

The generated password only contains alphanumeric characters and the following punctuation marks: !@#$%^&*()_-+=[{]};:<>|./?. No hidden or non-printable control characters are included in the generated password.

The random password created by the GeneratePassword method is not guaranteed to pass the regular expression in thePasswordStrengthRegularExpression property. However, the random password will meet the criteria established by the MinRequiredPasswordLengthproperty and the numberOfNonAlphanumericCharacters parameter.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s