PowerShell Random Word Generator

Today, I will explain everything about the “PowerShell random word generator” and show you how to generate random words in PowerShell using different approaches.

To generate random words in PowerShell using the Get-Random cmdlet with a predefined list, first create an array of words, such as @("apple", "banana", "cherry", "date", "elderberry", "fig", "grape"). Then, use Get-Random to select a random word from this array: $randomWord = $wordList | Get-Random. Finally, output the selected random word with Write-Output $randomWord.

Generate Random Words in PowerShell

Now, let me show you different methods to generate random words in PowerShell.

Method 1: Using Get-Random with a Predefined List

The best way to generate random words is by using the Get-Random cmdlet with a predefined list of words. Let me show you how this methods works with a simple example.

# Define a list of words
$wordList = @("apple", "banana", "cherry", "date", "elderberry", "fig", "grape")

# Get a random word from the list
$randomWord = $wordList | Get-Random

# Output the random word
Write-Output $randomWord

In this example, we define an array $wordList containing several words. The Get-Random cmdlet selects a random word from this list, which is then output using Write-Output.

I executed the above PowerShell script using VS code and you can see it generated random words like below in the screenshot.

Generate Random Words in PowerShell

Method 2: Generating Random Words from Characters

Let me now show you another method in PowerShell to generate random words. Here you can generate random words by combining random characters. This method allows you to specify the length and characters to use.

Let us implement one example. Here is the complete PowerShell script.

# Function to generate a random word
function Get-RandomWord {
    param (
        [int]$length = 5
    )
    
    $chars = "abcdefghijklmnopqrstuvwxyz".ToCharArray()
    $word = -join ((1..$length) | ForEach-Object { $chars | Get-Random })
    
    return $word
}

# Generate a random word of length 7
$randomWord = Get-RandomWord -length 7

# Output the random word
Write-Output $randomWord

In this example, we define a function Get-RandomWord that generates a random word of a specified length. The function uses the Get-Random cmdlet to select random characters from a string of lowercase letters.

You can see the output in the screenshot below, after I executed the script.

PowerShell Random Word Generator

Method 3: Using External Word Lists

You can use external word lists, such as dictionaries or text files containing words. This method can be particularly useful for generating more realistic or varied random words in PowerShell.

Here is an example. Suppose I have a .txt file contains a list of words. And then you can write a script like below to generate random words.

# Path to the external word list file
$wordListPath = "C:\MyFolder\wordlist.txt"

# Read the words from the file
$wordList = Get-Content -Path $wordListPath

# Get a random word from the list
$randomWord = $wordList | Get-Random

# Output the random word
Write-Output $randomWord

In this example, we read words from a text file using Get-Content, then select a random word from the list using Get-Random. Ensure the file path is correct and the file contains one word per line.

Method 4: Combine Words to Form Phrases

Let me show you another method which is a little different from all the above methods.

I will show you now, how to generate a random phrases or sentences than just single words. You can combine multiple random words to create phrases.

Here is an example how to do this.

# Define lists of adjectives and nouns
$adjectives = @("quick", "lazy", "sleepy", "noisy", "hungry")
$nouns = @("fox", "dog", "cat", "mouse", "bear")

# Get random adjective and noun
$randomAdjective = $adjectives | Get-Random
$randomNoun = $nouns | Get-Random

# Combine to form a phrase
$randomPhrase = "$randomAdjective $randomNoun"

# Output the random phrase
Write-Output $randomPhrase

In this example, we define two arrays, $adjectives and $nouns, containing adjectives and nouns, respectively. We then select a random adjective and noun, combine them to form a phrase, and output the result.

Here is the output you can see after I executed the above PowerShell script.

powershell random character generator

Conclusion

In this tutorial, I have explained different methods to generate random words in PowerShell such as: Using Get-Random with a Predefined List, Using External Word Lists, etc. I have also explained how to generating random words from characters in PowerShell. Still have questions? Leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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