How to Generate password automatically using Powershell

Generate  password using Powershell will be very easy.It will helps in the automation process which have Password field.

Get-Random

By default above funtion will generate 9 digit digital number(406284808) randomly.

We can use Get-Random funtion to generate the customized password as per our requirment.

Example:

  1. 6jbUs4:3PrZ@G<fc17?MYH2>RhxwXEyTd;8aOoB=VJ8vm      44-Digit password
  2. qMKeiA4h0bRsl     12-Digit password

We can customize the password with Alphabets,Digits and Numerics.

Example 1:

Code:

([char[]]([char]56..[char]64) + [char[]]([char]65..[char]90) + ([char[]]([char]97..[char]122)) + 0..9 | sort {Get-Random})[0..12] -join ”

Output:

WlRw<Td=Lb;GP

Above code will generate the 12digit password which contain all SpecialCharacters, Alphabets,Digits and Numerics.

ASCII Valuse which use for password generation

#65 -> 90 :: A -> Z   —— [char[]]([char]65..[char]90)
#97 -> 122 :: a -> z   —— ([char[]]([char]97..[char]122))
#56 -> 64 :: Special Characters  [char[]]([char]56..[char]64)

Example 2:

Code:

([char[]]([char]65..[char]90) + ([char[]]([char]97..[char]122)) + 0..9 | sort {Get-Random})[0..12] -join ”

Output:

8AWqOMFo7Llfk

Above code will generate the 12digit password which contain all Alphabets,Digits and Numerics.

 

Based on the length of the password , change the number.

{Get-Random})[0..12]   — 12 digit length password

{Get-Random})[0..24]  — 24 digit length password

 

Using above code in the script is very easy. Just assign the code to one variable and use the varibale.

$Password= ([char[]]([char]56..[char]64) + [char[]]([char]65..[char]90) + ([char[]]([char]97..[char]122)) + 0..9|
sort {Get-Random})[0..12] -join '' 


$Password

 

Generate Password

 

Note: To learn more about Get-Random function, Please click below link.
Click Me

Add a Comment

Your email address will not be published. Required fields are marked *