Scripting. Powershell, VBScript, Windows, Active Directory & Exchange.VMWare. All that kind of stuff…..
RSS icon Email icon Home icon
  • PowerShell 2.0: One Cmdlet at a Time #1 Get-Random

    Posted on November 12th, 2009 Jonathan Medd 2 comments

    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

    ;-)

    1000 things 1% better!


    2 Trackbacks / Pingbacks

    Leave a reply