Tag Archives: Minimum

Measure-Object

NAME
    Measure-Object

SYNOPSIS
    Calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text.

SYNTAX
    Measure-Object [-Average] [-Maximum] [-Minimum] [-Sum] [[-Property] <string[]>] [-InputObject <psobject>] [<CommonParameters>]

    Measure-Object [-Character] [-IgnoreWhiteSpace] [-Line] [-Word] [[-Property] <string[]>] [-InputObject <psobject>] [<CommonParameters>]

DESCRIPTION
    The Measure-Object cmdlet calculates the property values of certain types of object. Measure-Object performs three types of measurements, depending on the parameters in the command.
    The Measure-Object cmdlet performs calculations on the property values of objects. It can count objects and calculate the minimum, maximum, sum, and average of the numeric values. For text objects, it can count and calculate the number of lines, words, and characters.

PARAMETERS
    -Average [<SwitchParameter>]
        Displays the average value of the specified properties.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Character [<SwitchParameter>]
        Counts the number of characters in the input object.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -IgnoreWhiteSpace [<SwitchParameter>]
        Ignores white space in word counts and character counts. By default, white space is not ignored.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <psobject>
        Specifies the objects to be measured. Enter a Variable that contains the objects, or type a command or expression that gets the objects.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Line [<SwitchParameter>]
        Counts the number of lines in the input object.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Maximum [<SwitchParameter>]
        Displays the maximum value of the specified properties.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Minimum [<SwitchParameter>]
        Displays the minimum value of the specified properties.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Property <string[]>
        Specifies one or more numeric properties to measure. The default is the Count (Length) property of the object.

        Required?                    false
        Position?                    1
        Default value                Count
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Sum [<SwitchParameter>]
        Displays the sum of the values of the specified properties.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Word [<SwitchParameter>]
        Counts the number of words in the input object.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSObject
        You can pipe objects to Measure-Object.

OUTPUTS
    GenericMeasureInfo or TextMeasureInfoObject

NOTES

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-ChildItem | Measure-Object

    Description
    ———–
    This command counts the files and folders in the current directory.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-ChildItem | Measure-Object -Property length -Minimum -Maximum -Average

    Description
    ———–
    This command displays the minimum, maximum, and sum of the sizes of all files in the current directory, and the average size of a file in the directory.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Content C:\test.txt | Measure-Object -character -line -word

    Description
    ———–
    This command displays the number of characters, words, and lines in the Text.txt file.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-Process | Measure-Object -Property workingset -Minimum -Maximum -Average

    Description
    ———–
    This command displays the minimum, maximum, and average sizes of the working sets of the processes on the computer.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Import-Csv d:\test\serviceyrs.csv | Measure-Object -Property years -Minimum -Maximum -Average

    Description
    ———–
    This command calculates the average years of service of the employees of a company.

    The ServiceYrs.csv file is a CSV file that contains the employee number and years of service of each employee. The first row in the table is a header row of “EmpNo, Years”.

    When you use Import-Csv to import the file, the result is a PSCustomObject with note properties of EmpNo and Years. You can use Measure-Object to calculate the values of these properties, just like any other property of an object.

    ————————– EXAMPLE 6 ————————–

    C:\PS>Get-ChildItem | Measure-Object -Property psiscontainer -max -Sum -min -Average

    Count    : 126
    Average : 0.0634920634920635
    Sum     : 8
    Maximum : 1
    Minimum : 0
    Property : PSIsContainer

    Description
    ———–
    This example demonstrates the Measure-Object can measure Boolean values. In this case, it uses the PSIsContainer Boolean property to measure the incidence of folders (vs. files) in the current directory.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113349
    Compare-Object
    ForEach-Object
    Group-Object
    New-Object
    Select-Object
    Sort-Object
    Tee-Object
    Where-Object

Get-Random

NAME
    Get-Random

SYNOPSIS
    Gets a random number, or selects objects randomly from a collection.

SYNTAX
    Get-Random [-InputObject] <Object[]> [-Count <int>] [-SetSeed <int>] [<CommonParameters>]

    Get-Random [[-Maximum] <Object>] [-Minimum <Object>] [-SetSeed <int>] [<CommonParameters>]

DESCRIPTION
    The Get-Random cmdlet gets a randomly selected number. If you submit a collection of objects to Get-Random, it gets one or more randomly selected objects from the collection.

    Without parameters or input, a Get-Random command returns a randomly selected 32-bit unsigned integer between 0 (zero) and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647).

    You can use the parameters of Get-Random to specify a seed number, minimum and maximum values, and the number of objects returned from a submitted collection.

PARAMETERS
    -Count <int>
        Determines how many objects are returned. The default is 1. If the value of Count exceeds the number of objects in the collection, Get-Random returns all of the objects in random order.

        Required?                    false
        Position?                    named
        Default value                1
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <Object[]>
        Specifies a collection of objects. Get-Random gets randomly selected objects in random order from the collection. Enter the objects, a Variable that contains the objects, or a command or expression that gets the objects. You can also pipe a collection of objects to Get-Random.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Maximum <Object>
        Specifies a maximum value for the random number. Get-Random returns a value that is less than the maximum (not equal). Enter a 32-bit integer or a double-precision floating-point number, or an object that can be converted to an integer or double, such as a numeric string (“100”). The value of Maximum must be greater than (not equal to) the value of Minimum.

        If the value of Maximum or Minimum is a floating-point number, Get-Random returns a randomly selected floating-point number.

        If the value of Minimum is a double (a floating-point number), the default value of Maximum is Double.MaxValue. Otherwise, the default value is Int32.MaxValue (2,147,483,647 or 0x7FFFFFFF).

        Required?                    false
        Position?                    1
        Default value                Int32.MaxValue
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Minimum <Object>
        Specifies a minimum value for the random number. Enter a 32-bit integer or a double-precision floating-point number, or an object that can be converted to an integer or double, such as a numeric string (“100”). The default value is 0 (zero).

        The value of Minimum must be less than (not equal to) the value of Maximum. If the value of Maximum or Minimum is a floating-point number, Get-Random returns a randomly selected floating-point number.

        Required?                    false
        Position?                    named
        Default value                0
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SetSeed <int>
        Specifies a seed value for the random number generator. This seed value is used for the current command and for all subsequent Get-Random commands in the current session until you use SetSeed again or close the session. You cannot reset the seed to its default, clock-based value.

        The SetSeed parameter is not required. By default, Get-Random uses the system clock to generate a seed value. Because SetSeed results in non-random behavior, it is typically used only when trying to reproduce behavior, such as when debugging or analyzing a script that includes Get-Random commands.

        Required?                    false
        Position?                    named
        Default value                System clock
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Object
        You can pipe one or more objects to Get-Random. Get-Random selects values randomly from the piped objects.

OUTPUTS
    System.Object
        Get-Random returns an integer or floating-point number, or an object selected randomly from a submitted collection.

NOTES

        Get-Random sets a default seed for each session based on the system time clock when the session starts.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Random

    3951433

    Description
    ———–
    This command gets a random integer between 0 (zero) and Int32.MaxValue.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-Random -Maximum 100

    47

    Description
    ———–
    This command gets a random integer between 0 (zero) and 99.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Random -Minimum -100 -Maximum 100

    -56

    Description
    ———–
    This command gets a random integer between -100 and 99.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-Random -min 10.7 -max 20.93

    18.08467273887

    Description
    ———–
    This command gets a random floating-point number greater than or equal to 10.7 and less than 20.92.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Get-Random -input 1, 2, 3, 5, 8, 13

    8

    Description
    ———–
    This command gets a randomly selected number from the specified array.

    ————————– EXAMPLE 6 ————————–

    C:\PS>Get-Random -input 1, 2, 3, 5, 8, 13 -count 3

    3
    1
    13

    Description
    ———–
    This command gets three randomly selected numbers in random order from the array.

    ————————– EXAMPLE 7 ————————–

    C:\PS>Get-Random -input 1, 2, 3, 5, 8, 13 -count ([int]::MaxValue)

    2
    3
    5
    1
    8
    13

    Description
    ———–
    This command returns the entire collection in random order. The value of the Count parameter is the MaxValue static property of integers.

    To return an entire collection in random order, enter any number that is greater than or equal to the number of objects in the collection.

    ————————– EXAMPLE 8 ————————–

    C:\PS>Get-Random -input “red”, “yellow”, “blue”

    yellow

    Description
    ———–
    This command returns a random value from a non-numeric collection.

    ————————– EXAMPLE 9 ————————–

    C:\PS>Get-Process | Get-Random

    Handles NPM(K)    PM(K)     WS(K) VM(M) CPU(s)     Id ProcessName
    ——- ——    —–     —– —– ——     — ———–
        144     4     2080        488    36     0.48 3164 wmiprvse

    Description
    ———–
    This command gets a randomly selected process from the collection of processes on the computer.

    ————————– EXAMPLE 10 ————————–

    C:\PS>Get-Content servers.txt | Get-Random -count (Get-Content servers.txt).count | foreach {Invoke-Expression -computer $_ -command ‘Get-Process powershell’}

    Description
    ———–
    This command runs a command on a series of remote computers in random order.

    ————————– EXAMPLE 11 ————————–

    C:\PS>Get-Random -max 100 -SetSeed 23

    # Commands with the default seed are pseudorandom
    PS C:\ps-test> Get-Random -max 100
    59
    PS C:\ps-test> Get-Random -max 100
    65
    PS C:\ps-test> Get-Random -max 100
    21

    # Commands with the same seed are not random
    PS C:\ps-test> Get-Random -max 100 -SetSeed 23
    74
    PS C:\ps-test> Get-Random -max 100 -SetSeed 23
    74
    PS C:\ps-test> Get-Random -max 100 -SetSeed 23
    74

    # SetSeed results in a repeatable series
    PS C:\ps-test> Get-Random -max 100 -SetSeed 23
    74
    PS C:\ps-test> Get-Random -max 100
    56
    PS C:\ps-test> Get-Random -max 100
    84
    PS C:\ps-test> Get-Random -max 100
    46
    PS C:\ps-test> Get-Random -max 100 -SetSeed 23
    74
    PS C:\ps-test> Get-Random -max 100
    56
    PS C:\ps-test> Get-Random -max 100
    84
    PS C:\ps-test> Get-Random -max 100
    46

    Description
    ———–
    This example shows the effect of using the SetSeed parameter. Because SetSeed produces non-random behavior, it is typically used only to reproduce results, such as when debugging or analyzing a script.

    ————————– EXAMPLE 12 ————————–

    C:\PS>$files = dir -path c:\* -recurse

    C:\PS> $sample = $files | Get-Random -count 50

    Description
    ———–
    These commands get a randomly selected sample of 50 files from the C: drive of the local computer.

    ————————– EXAMPLE 13 ————————–

    C:\PS>Get-Random 10001

    7600

    Description
    ———–
    This command gets a random integer less than 10001. Because the Maximum parameter has position 1, you can omit the parameter name when the value is the first or only unnamed parameter in the command.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113446