Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Measure-Command

    Measures the time it takes to run script blocks and cmdlets.

    Syntax

    Default (Default)

    Measure-Command    [-InputObject <PSObject>]    [-Expression] <ScriptBlock>    [<CommonParameters>]

    Description

    TheMeasure-Command cmdlet runs a script block or cmdlet internally, times the execution of theoperation, and returns the execution time.

    Note

    Script blocks run byMeasure-Command run in the current scope, not a child scope.

    Examples

    Example 1: Measure a command

    This example measures the time it takes to run aGet-EventLog command that gets the events in theWindows PowerShell event log.

    Measure-Command { Get-EventLog "Windows PowerShell" }

    Example 2: Compare two outputs from Measure-Command

    The first command measures the time it takes to process a recursiveGet-ChildItem command thatuses thePath parameter to get only.txt files in theC:\Windows directory and itssubdirectories.

    The second command measures the time it takes to process a recursiveGet-ChildItem command thatuses the provider-specificFilter parameter.

    These commands show the value of using a provider-specific filter in PowerShell commands.

    Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }
    Days              : 0Hours             : 0Minutes           : 0Seconds           : 8Milliseconds      : 618Ticks             : 86182763TotalDays         : 9.9748568287037E-05TotalHours        : 0.00239396563888889TotalMinutes      : 0.143637938333333TotalSeconds      : 8.6182763TotalMilliseconds : 8618.2763
    Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}
    Days              : 0Hours             : 0Minutes           : 0Seconds           : 1Milliseconds      : 140Ticks             : 11409189TotalDays         : 1.32050798611111E-05TotalHours        : 0.000316921916666667TotalMinutes      : 0.019015315TotalSeconds      : 1.1409189TotalMilliseconds : 1140.9189

    Example 3: Piping input to Measure-Command

    Objects that are piped toMeasure-Command are available to the script block that is passed to theExpression parameter. The script block is executed once for each object on the pipeline.

    # Perform a simple operation to demonstrate the InputObject parameter# Note that no output is displayed.10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_; $i++) {$i} }
    Days              : 0Hours             : 0Minutes           : 0Seconds           : 0Milliseconds      : 12Ticks             : 122672TotalDays         : 1.41981481481481E-07TotalHours        : 3.40755555555556E-06TotalMinutes      : 0.000204453333333333TotalSeconds      : 0.0122672TotalMilliseconds : 12.2672

    Example 4: Displaying output of measured command

    To display output of expression inMeasure-Command you can use a pipe toOut-Default.

    # Perform the same operation as above adding Out-Default to every execution.# This will show that the ScriptBlock is in fact executing for every item.10, 20, 50 | Measure-Command -Expression {for ($i=0; $i -lt $_; $i++) {$i}; "$($_)" | Out-Default }
    102050Days              : 0Hours             : 0Minutes           : 0Seconds           : 0Milliseconds      : 11Ticks             : 113745TotalDays         : 1.31649305555556E-07TotalHours        : 3.15958333333333E-06TotalMinutes      : 0.000189575TotalSeconds      : 0.0113745TotalMilliseconds : 11.3745

    Example 5: Measuring execution in a child scope

    Measure-Command runs the script block in the current scope, so the script block can modify valuesin the current scope. To avoid changes to the current scope, you must wrap the script block inbraces ({}) and use the invocation operator (&) to execute the block in a child scope.

    $foo = 'Value 1'$null = Measure-Command { $foo = 'Value 2' }$foo$null = Measure-Command { & { $foo = 'Value 3' } }$foo
    Value 2Value 2

    For more information about the invocation operator, seeabout_Operators.

    Parameters

    -Expression

    Specifies the expression that is being timed. Enclose the expression in braces ({}).

    Parameter properties

    Type:ScriptBlock
    Default value:None
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:0
    Mandatory:True
    Value from pipeline:False
    Value from pipeline by property name:False
    Value from remaining arguments:False

    -InputObject

    Objects bound to theInputObject parameter are optional input for the script block passed to theExpression parameter. Inside the script block,$_ can be used to reference the current objectin the pipeline.

    Parameter properties

    Type:PSObject
    Default value:None
    Supports wildcards:False
    DontShow:False

    Parameter sets

    (All)
    Position:Named
    Mandatory:False
    Value from pipeline:True
    Value from pipeline by property name:False
    Value from remaining arguments:False

    CommonParameters

    This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,-InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable,-ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, seeabout_CommonParameters.

    Inputs

    PSObject

    You can pipe an object to this cmdlet.

    Outputs

    TimeSpan

    This cmdlet returns a time span object representing the result.

    Related Links

    Collaborate with us on GitHub
    The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

    Feedback

    Was this page helpful?

    YesNo

    In this article

    Was this page helpful?

    YesNo