How to get the execution time of a cmdlet in PowerShell


You have two ways of getting the execution time for a cmdlet in PowerShell

  1. Measure-Command
  2. Get-History

The first option run the command, and hide the native output to display execution time details. So, if you ran a command, and you want to know, afterward, how long it takes to run, you must run it again inside the “Measure-Command” script block.

Measure-Command { Test-Connection -ComputerName -Count 25}


I personally prefer the second option because you don’t need to run the commands again, and you can compare different cmdlets more easily, that’s nice for script’s performances optimizations.

Get-History | Select-Object CommandLine,@{Label="Duration Time";Expression={$_.EndExecutionTime - $_.StartExecutionTime}}


Of course, you can use both cmdlets on scripts or advanced functions as well.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.