Share via


Measure-Command and messages screen output


The correct way to write messages to the Console is using Write-Host Cmdlet. More details about it you can be found here.

If you are a lazy scripting guy, you will put directly the text without Write-Host Cmdlet. Of course, no formatting text available, but the message is transmitted and it will be on the screen.

But what if you would like to measure your script, to know how many seconds or minutes took to run?

You could use this cmdlet Measure-Command, which will output the total duration as presented bellow.

>
Measure-Command {
sleep 10
}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 10
Milliseconds      : 2
Ticks             : 100023127
TotalDays         : 0,000115767508101852
TotalHours        : 0,00277842019444444
TotalMinutes      : 0,166705211666667
TotalSeconds      : 10,0023127
TotalMilliseconds : 10002,3127

The issue is regarding how this Measure-Command cmdlet manages output to the console of the messages.

For the line with Write-Host will be no problems, but if in your code You've used the lines without Write-Host, the text/message will be not transmitted to the console. So it is recommended to use Write-Host, even it takes longer to execute the lines.

( for example using write-host

> Measure-Command {
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
Write-host " This message is output with write-host cmdlet, for 10 times"
}

---------

 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times
 This message is output with write-host cmdlet, for 10 times

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 26
Ticks             : 262341
TotalDays         : 3,03635416666667E-07
TotalHours        : 7,28725E-06
TotalMinutes      : 0,000437235
TotalSeconds      : 0,0262341
TotalMilliseconds : 26,2341

versus writing directly to the console

>
$i = Get-Date
**" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
" This message is output without write-host cmdlet, for 10 times"
$f = Get-Date
$f-$i
**---------
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times
 This message is output without write-host cmdlet, for 10 times

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 17
Ticks             : 171361
TotalDays         : 1,98334490740741E-07
TotalHours        : 4,76002777777778E-06
TotalMinutes      : 0,000285601666666667
TotalSeconds      : 0,0171361
TotalMilliseconds : 17,1361

)