PowerShell Examples: Generating Random Names

This blog is part of a series that shows example PowerShell code for those learning the language.

This time we’re using PowerShell to generate random full names using common first names and common last names. It basically combines a random first name with a random letter for middle initial and a random last name.

This example explores the use of loops, random numbers, string functions (split, padright) and conversion from [int] to [char].


# Creating common names
# Using the top first names published by the SSA at
# And the top last names from http://en.wikipedia.org/wiki/List_of_most_common_surnames_in_North_America

$first = “Noah Sophia Liam Emma Jacob Olivia Mason Isabella William Ava Ethan Mia Michael Emily Alexander Abigail Jayden Madison Daniel Elizabeth”.Split(” “)
$last = “Smith Johnson Williams Brown Jones Miller Davis Garcia Rodriguez Wilson Martinez Anderson Taylor Thomas Hernandez Moore Martin Jackson Thompson White”.Split(” “)

Write-Host “Random full name generator”

1..20 | foreach {
    $f = $first[ (Get-Random $first.count ) ]
    $m = [char] (65 + (Get-Random 26) )
    $l = $last[ (Get-Random $last.count) ]
    $full = $f+” “+$m+” “+$l
    Write-Host -NoNewline $full.PadRight(25)
    If ($_ % 2 -eq 0) {
        Write-Host “”

# Sample output
# Random full name generator
# Emily B Miller           Olivia N Hernandez      
# Mia I Jones              Olivia W Miller         
# Noah M Williams          Daniel V Thomas         
# William Y Martin         Elizabeth A Thompson    
# Mia A Martinez           Abigail G Johnson       
# Ethan I Williams         Noah L Taylor           
# Madison K Rodriguez      Michael Z Smith         
# Mason B Thompson         William I Thomas        
# Mia U Smith              Emily R Taylor          
# Daniel Q Wilson          Elizabeth R Taylor      

Comments (0)

Skip to main content