How to Generate Random Strings in PowerShell?

Recently, a system administrator asked me about generating random strings in PowerShell. I explained different examples. In this tutorial, I will explain how to generate random strings in PowerShell with a few examples.

To generate a random string in PowerShell using the Get-Random cmdlet, define the desired string length and a set of characters to choose from. Use a loop to select random characters from this set and join them into a string. For example, to create a 10-character string, you can use the following script:

$length = 10
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
$randomString = -join ((1..$length) | ForEach-Object { $characters | Get-Random })
Write-Output $randomString

This script generates a random string by selecting characters randomly from the specified set and concatenating them.

Generate Random Strings in PowerShell

Random strings are sequences of characters generated in such a way that each character is selected independently of the others.

The simplest way to generate a random string in PowerShell is by using the Get-Random cmdlet. This cmdlet allows you to select random objects from a collection, which can be used to build a random string.

Now, let me show you a few examples:

Example-1: Generate a Random String of Fixed Length

Here is a simple example of generating a random string of 10 characters in PowerShell. Below is the complete PowerShell script.

# Define the length of the random string
$length = 10

# Define the set of characters to choose from
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.ToCharArray()

# Generate the random string
$randomString = -join ((1..$length) | ForEach-Object { $characters | Get-Random })

# Output the random string
Write-Output $randomString

Once you execute the above script, you can see the output in the screenshot below:

Generate Random Strings in PowerShell

Check out Generate Random Numbers in PowerShell

Example-2: Generate a Random String with Uppercase Letters

Here’s a basic example of generating a random string consisting of uppercase letters in PowerShell:

# Define the characters to use
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()

# Initialize an empty string
$randomString = ""

# Generate a random string of length 10
for ($i = 0; $i -lt 10; $i++) {
    $randomChar = $chars | Get-Random
    $randomString += $randomChar
}

Write-Output $randomString

In this script, we define a string $chars containing all the uppercase letters. We then loop 10 times, each time selecting a random character from $chars using the Get-Random cmdlet and appending it to $randomString.

I executed the above PowerShell script, and you can see the output in the screenshot below:

powershell generate random string

Generate Random Strings in PowerShell with Custom Functions

You can also create a custom function in PowerShell to generate a random string, and you can reuse the function.

Here’s a custom function that generates a random string of a specified length:

function Get-RandomString {
    param (
        [int]$Length = 10,
        [string]$Characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    )

    # Convert the string of characters into an array of characters
    $charArray = $Characters.ToCharArray()

    # Generate the random string
    $randomString = -join ((1..$Length) | ForEach-Object { $charArray | Get-Random })

    return $randomString
}

# Generate a random string with the default length (10)
$randomString = Get-RandomString
Write-Output $randomString

# Generate a random string with a custom length (15)
$randomString = Get-RandomString -Length 15
Write-Output $randomString

This function allows you to specify the length of the random string and the set of characters to use. By default, it generates a string of 10 characters.

You can also see the exact output in the screenshot below:

Generate Random Strings in PowerShell with Custom Functions

Check out PowerShell Random Password Generator

Generate Random Strings with Special Characters in PowerShell

Sometimes, you might need to generate random strings with special characters in PowerShell. Below is the complete PowerShell script.

# Define the characters to use, including special characters
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/`~".ToCharArray()

# Initialize an empty string
$randomString = ""

# Generate a random string of length 15
for ($i = 0; $i -lt 15; $i++) {
    $randomChar = $chars | Get-Random
    $randomString += $randomChar
}

Write-Output $randomString

In this example, the $chars string now includes lowercase letters, digits, and a variety of special characters, and is converted to an array. The loop generates a random string of length 15 by selecting characters from this extended set.

You can see the output in the screenshot below; it generated a random string that contains special characters.

Generate Random Strings with Special Characters in PowerShell

Check out PowerShell Random Word Generator

Generate Random Strings with Letters and Numbers in PowerShell

Let me show you another common requirement: generating random strings that include letters and numbers. This can be useful for creating alphanumeric identifiers.

Here’s how you can generate a random string containing both letters and numbers:

# Define the characters to use, including letters and numbers
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray()

# Initialize an empty string
$randomString = ""

# Generate a random string of length 12
for ($i = 0; $i -lt 12; $i++) {
    $randomChar = $chars | Get-Random
    $randomString += $randomChar
}

Write-Output $randomString

In this script, the $chars string includes both uppercase and lowercase letters as well as digits, and is converted to an array. The loop generates a random string of length 12 by selecting characters from this set.

Here is the output in the screenshot below:

Generate Random Strings with Letters and Numbers in PowerShell

Generate a random string of Specific Character Types in PowerShell

Sometimes, you may need to ensure that the generated random string includes at least one character from each specified type (e.g., at least one letter, one number, and one special character). Here’s how you can achieve that:

function New-SecureRandomString {
    param (
        [int]$length = 12
    )

    # Define character sets
    $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray()
    $numbers = "0123456789".ToCharArray()
    $specialChars = "!@#$%^&*()-_=+[]{}|;:,.<>?/`~".ToCharArray()

    # Ensure at least one character from each set
    $randomString = $letters | Get-Random -Count 1
    $randomString += ($numbers | Get-Random -Count 1)
    $randomString += ($specialChars | Get-Random -Count 1)

    # Combine all character sets
    $allChars = $letters + $numbers + $specialChars

    # Generate the remaining characters
    for ($i = 3; $i -lt $length; $i++) {
        $randomChar = $allChars | Get-Random
        $randomString += $randomChar
    }

    # Shuffle the string
    $randomString = ($randomString.ToCharArray() | Get-Random -Count $randomString.Length) -join ''

    return $randomString
}

# Generate a secure random string of length 16
Write-Output (New-SecureRandomString -length 16)

In this function, New-SecureRandomString, we ensure that the generated string includes at least one letter, one number, and one special character. We then fill the remaining length with random characters from the combined set and shuffle the string to ensure randomness.

Here is the output in the screenshot below:

Generate a random string of Specific Character Types in PowerShell

Conclusion

In this tutorial, I have explained how to generate random strings in PowerShell using Get-Random cmdlet. I have also shown a few different examples like:

  • Generate Random Strings in PowerShell with Custom Functions
  • Generate Random Strings with Special Characters in PowerShell
  • Generate Random Strings with Letters and Numbers in PowerShell
  • Generate a random string of Specific Character Types in PowerShell

Do you still have questions? Feel free to leave a comment below:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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