How to get the execution time of a cmdlet in PowerShell

Hello,

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 192.168.1.254 -Count 25}

Measure-Command

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}}

Get-History

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

 

Leave a Reply