Posted on August 11, 2016 · Posted in Powershell

Generating Random Password with PowerShell

It is not easy to think a secure password, so that it meet certain requirements to the length and complexity. There are many programs and online services in the Internet to generate passwords, but why using third-party programs if you can do with the tools that already exist in your system. For example, you can generate random passwords using PowerShell.

To generate a password, you can try GeneratePassword method used by system to reset a user password and create a new temporary password. The length of the generated password is between 1 to 128 symbols, it may include letters, numbers and special symbols (!,@,$,%,&, etc.).

To generate a password, firstly add the System.Web assembly using Add-Type cmdlet:

Add-Type -AssemblyName System.Web

Then generate your password with the command:


Numbers in brackets mean that the total password length is 10 symbols with 4 non-alphabetic characters.

Generate Random Password PowerShell

In our example, PowerShell generates the following complex password: hCqL=e?}W.

If you have to create new passwords often, you can use the following script:

$PasswordLength = Read-Host 'Password length (1 - 128)'
$NonAlphCh = Read-Host 'The number of Non-alphabetic characters'
Add-Type -AssemblyName System.Web

After specifying the necessary parameters, run it to get a password.

PoSh Script to Generate Random password

Here is how you can generate a random password of any level of complexity using PowerShell with no need to use any third-party software.

Related Articles