PowerShell 2.0: One Cmdlet at a Time 1 Get-Random
This is the first of a series looking at new cmdlets available in PowerShell 2.0. We begin by looking at the Get-Random cmdlet.
What can I do with it?
With Get-Random you can either generate a random number, or randomly select objects from a collection.
Examples:
Generate a random number between 1 and 100.
Get-Random -Minimum 1 -Maximum 101
Select a random object from a collection
$users = ‘Rod’,‘Jane’,‘Freddy’ Get-Random $users
Select a random Windows Service
Get-Service | Get-Random
How could I have done this in PowerShell 1.0?
Generate a random number between 1 and 100 using .NET.
$randomnumber = New-Object System.Random $randomnumber.next(1,101)
Select a random object from a collection
$users = ‘Rod’,‘Jane’,‘Freddy’ $randomnumber = New-Object System.Random $i = $randomnumber.next(0,$users.length) $users[$i]
How did I decide to begin this series with Get-Random?
Get-Command | Get-Random
;-)