I was considering installing the timeit.exe command on my new laptop, but read that it may not work on the latest versions of Windows. So, I found an article on StackOverflow that recommended using the Measure-Command command in PowerShell. You use it as follows, where Get-ChildItem is just listing the current directory as an example command to time:
1 |
PS C:\> Measure-Command { Get-ChildItem }<br /><br />Days : 0<br />Hours : 0<br />Minutes : 0<br />Seconds : 0<br />Milliseconds : 28<br />Ticks : 288474<br />TotalDays : 3.33881944444444E-07<br />TotalHours : 8.01316666666667E-06<br />TotalMinutes : 0.00048079<br />TotalSeconds : 0.0288474<br />TotalMilliseconds : 28.8474 |
If you just want to see one of these results, you can specify the field, such as milliseconds, as follows:
1 |
PS C:\> (Measure-Command { Get-EventLog "windows powershell" }).Milliseconds<br /><br />305 |
Note that Measure-Command does not show you the output of the command that you have executed for PowerShell commands, just the time it took to execute the command. If you want to see the command’s output, pipe it to Out-Default, as follows:
1 |
PS C:\> Measure-Command { Get-EventLog "windows powershell" | Out-Default } |
If you want to use it to execute cmd.exe commands vs. PowerShell commands, prefix the command with “cmd /c” as follows:
1 |
PS C:\> Measure-Command { cmd /c dir /s c:\windows > nul } |
Thanks to Casey K and TechGibbon for the useful information.
Rob