PowerShell does not natively support this, but if you need to trace out a number of commands to see which is the slowest in a script, you could use a construct like this one:
$commands = @(
'write-host 123',
'write-host 234',
'set-location c:\git',
'write-host 123',
'set-location c:\temp'
)
forEach ($c in $commands){
$stopWatch = Measure-command -Expression {Invoke-Expression $c}
Write-host "The last command [$c] was executed in [$($stopWatch.TotalMilliseconds)]"
}
This will execute all of commands within the array $commands one by one and run Measure-Command on them, which returns a rich TimeSpan object that has the TotalMilliseconds field you were looking to use. Outputs like this:
The last command [write-host 123] was executed in [2.3979]
The last command [write-host 234] was executed in [0.031]
The last command [set-location c:\git] was executed in [0.0236]
The last command [write-host 123] was executed in [0.021]
The last command [set-location c:\temp] was executed in [0.0171]
The snippet of code could be modified to work with a script too, so if we imagine we had a script like this:
#myCoolScript.ps1
write-host 123
start-sleep -Seconds 2
write-host 234
start-sleep -Seconds 1
set-location c:\git
write-host 123
set-location c:\temp
You could modify the code like so to measure out each line:
$commands = get-content .\MyCoolScript.ps1
forEach ($c in $commands){
$stopWatch = Measure-command -Expression {invoke-expression $c}
Write-host "The last command [$c] was executed in [$($stopWatch.TotalMilliseconds)]"
}
Which would give this output:
The last command [write-host 123] was executed in [10.1866]
The last command [start-sleep -Seconds 2] was executed in [2000.178]
The last command [write-host 234] was executed in [1.1301]
The last command [start-sleep -Seconds 1] was executed in [999.5883]
The last command [set-location c:\git] was executed in [0.5302]
The last command [write-host 123] was executed in [0.9388]
The last command [set-location c:\temp ] was executed in [0.3985]