CmdID / PowerShell / Cmdlets / Measure
Measure-Command
Measures the time it takes to run scriptblocks and cmdlets. PowerShell reference
From the PowerShell reference 2 parameters
What it does
Measures the time it takes to run scriptblocks and cmdlets. The Measure-Command cmdlet runs a scriptblock or cmdlet internally, times the execution of the operation, and returns the execution time. NOTE: scriptblocks run by Measure-Command run in the current scope, not a child scope.
Measure-Command [-InputObject <PSObject>] [-Expression] <ScriptBlock> [<CommonParameters>]
Measure a command
Measure-Command { Get-EventLog "Windows PowerShell" }IdentifyTrips 2 Sigma rules: mediumEventLog Query Requests By Builtin Utilities by Ali Alwashali, Nasreddine Bencherchali (Nextron Systems); mediumPotentially Suspicious EventLog Recon Activity Using Log Query Utilities by Nasreddine Bencherchali (Nextron Systems), X__Junior (Nextron Systems)
Compare two outputs from Measure-Command
Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }IdentifyPiping 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} }IdentifyDisplaying 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 }IdentifyMeasuring execution in a child scope
$foo = 'Value 1'
$null = Measure-Command { $foo = 'Value 2' }
$foo
$null = Measure-Command { & { $foo = 'Value 3' } }
$fooIdentifyIts page in the reference: Measure-Command.
Parameters
-ExpressionSpecifies the expression that is being timed. Enclose the expression in braces ({}).-InputObjectObjects bound to the InputObject parameter are optional input for the scriptblock passed to the Expression parameter. Inside the scriptblock, $_ can be used to reference the current object in the pipeline.