This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Measures the time it takes to run script blocks and cmdlets.
Measure-Command [-InputObject <PSObject>] [-Expression] <ScriptBlock> [<CommonParameters>]
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.
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" }
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
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
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
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.
Specifies the expression that is being timed. Enclose the expression in braces ({}
).
Type: | ScriptBlock |
Default value: | None |
Supports wildcards: | False |
DontShow: | False |
Position: | 0 |
Mandatory: | True |
Value from pipeline: | False |
Value from pipeline by property name: | False |
Value from remaining arguments: | False |
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.
Type: | PSObject |
Default value: | None |
Supports wildcards: | False |
DontShow: | False |
Position: | Named |
Mandatory: | False |
Value from pipeline: | True |
Value from pipeline by property name: | False |
Value from remaining arguments: | False |
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,-InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable,-ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, seeabout_CommonParameters.
You can pipe an object to this cmdlet.
This cmdlet returns a time span object representing the result.
Was this page helpful?
Was this page helpful?