Measure-Command
SYNOPSIS
Measures the time it takes to run script blocks and cmdlets.
SYNTAX
Measure-Command [-InputObject <PSObject>] [-Expression] <ScriptBlock> [<CommonParameters>]
DESCRIPTION
The `Measure-Command` cmdlet runs a script block or cmdlet internally, times the execution of the operation, and returns the execution time.
> [!NOTE] > Script blocks run by `Measure-Command` run in the current scope, not a child scope.
EXAMPLES
Example 1: Measure a command
Measure-Command { Get-EventLog "windows powershell" }
Example 2: Compare two outputs from Measure-Command
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
Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}
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
Example 3: Piping input to Measure-Command
# 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 : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 12
Ticks : 122672
TotalDays : 1.41981481481481E-07
TotalHours : 3.40755555555556E-06
TotalMinutes : 0.000204453333333333
TotalSeconds : 0.0122672
TotalMilliseconds : 12.2672
Example 4: Displaying output of measured command
# 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 }
10
20
50
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 11
Ticks : 113745
TotalDays : 1.31649305555556E-07
TotalHours : 3.15958333333333E-06
TotalMinutes : 0.000189575
TotalSeconds : 0.0113745
TotalMilliseconds : 11.3745
Example 5: Measuring execution in a child scope
$foo = 'Value 1'
$null = Measure-Command { $foo = 'Value 2' }
$foo
$null = Measure-Command { & { $foo = 'Value 3' } }
$foo
Value 2
Value 2
For more information about the invocation operator, see about_Operators (../Microsoft.PowerShell.Core/About/about_Operators.md#call-operator-).
PARAMETERS
-Expression
Specifies the expression that is being timed. Enclose the expression in braces (`{}`).
Type: System.Management.Automation.ScriptBlock
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-InputObject
Objects bound to the InputObject parameter are optional input for the script block passed to the Expression parameter. Inside the script block, `$_` can be used to reference the current object in the pipeline.
Type: System.Management.Automation.PSObject
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
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.