PowerShell Random Password Generator

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 $password

In this script:

  • We define a function New-RandomPassword that accepts a parameter for the password length.
  • The $characters variable contains all possible characters for the password.
  • We use a ForEach-Object loop to select random characters from the $characters string 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.

powershell generate random password

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 $password

In 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 $password

In this script:

  • We use RNGCryptoServiceProvider to generate cryptographically secure random bytes.
  • These bytes are then mapped to the characters in the $chars string 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 $password

In 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:

powershell random password generator

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 $password

In this script:

  • We define a function New-RandomPassword that accepts a parameter for the password length.
  • The $characters variable contains all possible characters for the password (excluding special characters).
  • We use a ForEach-Object loop to select random characters from the $characters string 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.

powershell generate random password without special characters

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 $password

In 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 $password

In this script:

  • We define a function New-RandomPassword that accepts a parameter for the password length.
  • The $characters variable contains all possible characters for the password, including special characters.
  • We use a ForEach-Object loop to select random characters from the $characters string and join them into a password.

The output is in the screenshot below. This random password contains special characters.

powershell generate random password with 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 $password

In this example:

  • The GeneratePassword method 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
100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.