How to generate a strong password using PowerShell?

To generate a strong password with the PowerShell, we can use 8 or more characters password. In the below example, we are going to use the 8 character password, you can change the number of characters as per your convenience.

This script you can bind into windows forms and provide it to the IT helpdesk to set the password for new users.

In this example, we will use the Random integers between 33 and 126 and convert it to the Character, considering them they are the ASCII values for characters.

You can find more about ASCII values in the table provided in the link.

Now we will see the explanation of the script and then finally write the script.

To get the random numbers between 33 and 126, Get-Random command is used.

Get-Random -Minimum 33 -Maximum 126

Once, you get the number, translate it into character by type conversion.

[char](Get-Random -Minimum 33 -Maximum 126)

We have not a single character, but we need 8 characters long password so you need to loop above commands 8 times and append each character to the previous character.


[int]$passlength = 8
for($i=0; $i -lt $passlength; $i++){
   $pass += [char](Get-Random -Minimum 33 -Maximum 126)



Updated on: 13-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started