Skip to content
PowerShell Commands

Measure-Command

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

The Measure-Command cmdlet runs a script block or cmdlet internally, times the execution of the operation, and returns the execution time.

Parameters

-Expression <ScriptBlock>

  • This value is required
  • Default value is None
  • Accepts pipeline input False

Specifies the expression that is being timed. Enclose the expression in braces ({}). The parameter name ("Expression") is optional.

-InputObject <PSObject>

  • Default value is None
  • Accepts pipeline input ByValue

Specifies objects representing the expressions to be measured. Enter a variable that contains the objects or type a command or expression that gets the objects.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs
System.Management.Automation.PSObject
You can pipe an object to Measure-Command .
Outputs
System.TimeSpan
Measure-Command returns a time span object that represents the result.
Examples
  1. Measure the time to run a command:
    PS C:\> Measure-Command { Get-EventLog "windows powershell" }
    

    This command measures the time it takes to run a Get-EventLog command that gets the events in the Windows PowerShell event log.

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

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

    The first command measures the time it takes to process a recursive Get-ChildItem command that uses the Path parameter to get only .txt files in the C:\Windows directory and its subdirectories.

    The second command measures the time it takes to process a recursive Get-ChildItem command that uses the provider-specific Filter parameter.

Additional Notes
 * For more information, type `Get-Help Measure-Command -detailed`. For technical information, type `Get-Help 
 Measure-Command -full`.

 When specifying multiple values for a parameter, use commas to separate the values. For example, 
  , .

 *

This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.

PowerShell Commands