Recently, one of my clients asked me to create a random password generator. I suggested PowerShell for it. In this tutorial, I will show you how to create a random password generator using PowerShell.
Generate Random Password Using PowerShell
Now, let me show you different methods to generate random passwords using PowerShell.
Method 1: Using the Get-Random Cmdlet
The Get-Random cmdlet in PowerShell can be used to generate random characters and assemble them into a password. Here’s a step-by-step example:
function New-RandomPassword {
param (
[int]$length = 12
)
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'
$password = -join ((1..$length) | ForEach-Object { $characters[(Get-Random -Minimum 0 -Maximum $characters.Length)] })
return $password
}
$password = New-RandomPassword -length 16
Write-Output $passwordIn this script:
- We define a function
New-RandomPasswordthat accepts a parameter for the password length. - The
$charactersvariable contains all possible characters for the password. - We use a
ForEach-Objectloop to select random characters from the$charactersstring and join them into a password.
Once you execute the above PowerShell script, it will generate a random password, as shown in the screenshot below. I used Visual Studio Code to execute these scripts.

Check out Generate Random Strings in PowerShell
Method 2: Using the System.Web.Security.Membership Class
The System.Web.Security.Membership class provides another way to generate random passwords. Here’s how you can use it:
Add-Type -AssemblyName System.Web
$password = [System.Web.Security.Membership]::GeneratePassword(12, 2)
Write-Output $passwordIn this example, the GeneratePassword() method takes two parameters:
- The first parameter specifies the length of the password.
- The second parameter specifies the number of non-alphanumeric characters.
This method is simple and effective for generating secure passwords quickly, but it requires adding the assembly.
Method 3: Using SecureRandom from .NET
For an even more secure approach, you can use the System.Security.Cryptography namespace to generate random passwords. Here’s an example:
function New-SecureRandomPassword {
param (
[int]$length = 12
)
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'
$bytes = New-Object byte[] $length
[System.Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($bytes)
$password = -join ($bytes | ForEach-Object { $chars[$_ % $chars.Length] })
return $password
}
$password = New-SecureRandomPassword -length 16
Write-Output $passwordIn this script:
- We use
RNGCryptoServiceProviderto generate cryptographically secure random bytes. - These bytes are then mapped to the characters in the
$charsstring to form the password.
Check out How to Generate Random Numbers in PowerShell?
Generate Complex Passwords using Different Character Sets in PowerShell
This is a very common requirement where you might required to generate complex passwords using different character sets in PowerShell.
To ensure that your passwords include a mix of character types (uppercase, lowercase, digits, symbols), you can combine different character sets and shuffle them. Here’s an example:
function New-ComplexPassword {
param (
[int]$length = 12
)
$lower = 'abcdefghijklmnopqrstuvwxyz'
$upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$digits = '0123456789'
$symbols = '!@#$%^&*()'
$allChars = $lower + $upper + $digits + $symbols
$password = -join ((1..$length) | ForEach-Object { $allChars[(Get-Random -Minimum 0 -Maximum $allChars.Length)] })
# Ensure at least one character from each set
$password += $lower[(Get-Random -Minimum 0 -Maximum $lower.Length)]
$password += $upper[(Get-Random -Minimum 0 -Maximum $upper.Length)]
$password += $digits[(Get-Random -Minimum 0 -Maximum $digits.Length)]
$password += $symbols[(Get-Random -Minimum 0 -Maximum $symbols.Length)]
# Shuffle the password
$password = -join ($password.ToCharArray() | Sort-Object { Get-Random })
return $password.Substring(0, $length)
}
$password = New-ComplexPassword -length 16
Write-Output $passwordIn this script:
- We define separate character sets for lowercase, uppercase, digits, and symbols.
- We ensure the password includes at least one character from each set.
- We shuffle the characters to randomize the order.
I executed the above PowerShell script, and you can see the output in the screenshot below:

Check out PowerShell Random Word Generator
Generate Random Password Without Special Characters in PowerShell
Sometimes, you do not want to have any special characters while generating a random password in PowerShell.
The Get-Random cmdlet can be used to generate random characters from a specified set. Here’s an example of how to generate a random password without special characters:
function New-RandomPassword {
param (
[int]$length = 12
)
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
$password = -join ((1..$length) | ForEach-Object { $characters[(Get-Random -Minimum 0 -Maximum $characters.Length)] })
return $password
}
$password = New-RandomPassword -length 16
Write-Output $passwordIn this script:
- We define a function
New-RandomPasswordthat accepts a parameter for the password length. - The
$charactersvariable contains all possible characters for the password (excluding special characters). - We use a
ForEach-Objectloop to select random characters from the$charactersstring and join them into a password.
Here is the output, as you can see in the screenshot below. The random password does not contain a special character.

You can also use different character sets without any special characters in PowerShell.
To ensure that your passwords include a mix of character types (uppercase, lowercase, digits) without special characters, you can combine different character sets and shuffle them. Here’s an example:
function New-ComplexPassword {
param (
[int]$length = 12
)
$lower = 'abcdefghijklmnopqrstuvwxyz'
$upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$digits = '0123456789'
$allChars = $lower + $upper + $digits
$password = -join ((1..$length) | ForEach-Object { $allChars[(Get-Random -Minimum 0 -Maximum $allChars.Length)] })
# Ensure at least one character from each set
$password += $lower[(Get-Random -Minimum 0 -Maximum $lower.Length)]
$password += $upper[(Get-Random -Minimum 0 -Maximum $upper.Length)]
$password += $digits[(Get-Random -Minimum 0 -Maximum $digits.Length)]
# Shuffle the password
$password = -join ($password.ToCharArray() | Sort-Object { Get-Random })
return $password.Substring(0, $length)
}
$password = New-ComplexPassword -length 16
Write-Output $passwordIn this script:
- We define separate character sets for lowercase, uppercase, and digits.
- We ensure the password includes at least one character from each set.
- We shuffle the characters to randomize the order.
Generate Random Password with Special Characters in PowerShell
Now, let me show you how to generate random passwords with special characters in PowerShell.
The Get-Random cmdlet in PowerShell can be used to generate random characters and assemble them into a password. Here’s a step-by-step example that includes special characters:
function New-RandomPassword {
param (
[int]$length = 12
)
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()'
$password = -join ((1..$length) | ForEach-Object { $characters[(Get-Random -Minimum 0 -Maximum $characters.Length)] })
return $password
}
$password = New-RandomPassword -length 16
Write-Output $passwordIn this script:
- We define a function
New-RandomPasswordthat accepts a parameter for the password length. - The
$charactersvariable contains all possible characters for the password, including special characters. - We use a
ForEach-Objectloop to select random characters from the$charactersstring and join them into a password.
The output is in the screenshot below. This random password contains special characters.

You can also use the Using the System.Web.Security.Membership Class to generate a random password with special characters.
The System.Web.Security.Membership class provides an easy way to generate random passwords that include special characters. Here’s how you can use it:
Add-Type -AssemblyName System.Web
$password = [System.Web.Security.Membership]::GeneratePassword(12, 2)
Write-Output $passwordIn this example:
- The
GeneratePasswordmethod takes two parameters: the length of the password and the number of non-alphanumeric characters (special characters). - This method is simple and effective for quickly generating secure passwords.
Conclusion
In this tutorial, I have explained everything about the “PowerShell random password generator“. Here, I have explained how to generate random passwords in PowerShell using different methods. I have also explained:
- Generate a random password without special characters in PowerShell
- Generate a random password with special characters in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.