September 15th, 2018

PowerTip: Use PowerShell to pick a random name from a list

Doctor Scripto
Scripter

Summary: Using the Get-Random Cmdlet to select a random list of names instead of numbers

Hey! Dr. Scripto!

I’d like to pick a random name from a list to generate user names in my lab.   Could you show me an example?

I’d be delighted to.   Just use an array of names and then add them as a parameter to Get-Random.   You could even pipe it to Get-Random if you liked as well.

$NameList=’John’,’Charlotte’,’Sean’,’Colleen’,’Namoli’,’Maura’,’Neula’

Get-Random -InputObject $NameList

Drawing of Dr. Scripto

Windows PowerShell, Sean Kearney, Scripting Guy, PowerTip

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • Robbie Crash

    Is there a reason, other than just to be explicit, to specify -InputObject? Rather than say Get-Random $NameList or Get-Random ’John’,’Charlotte’,’Sean’,’Colleen’,’Namoli’,’Maura’,’Neula’

    • Nikolay Kozhemyak

      It is a best practice to type the full name of cmdlets and parameters within the script body.