How to use the command Measure-Command (with examples)

How to use the command Measure-Command (with examples)

Measure-Command is a command available in PowerShell that measures the time it takes to run script blocks and cmdlets. It is useful for measuring the performance of different commands or scripts in order to optimize and improve their execution time.

Use case 1: Measure the time it takes to run a command

Code:

Measure-Command { command }

Motivation: The motivation for using this example is to measure the execution time of a specific command in PowerShell. This can be useful for identifying bottlenecks in your scripts or commands and optimizing their performance.

Explanation:

  • Measure-Command: This is the command that measures the time it takes to run a script block or cmdlet.
  • { command }: This is the script block or cmdlet that you want to measure. Replace “command” with the actual command you want to measure.

Example output:

Measure-Command { Get-Process }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 157
Ticks             : 1578979
TotalDays         : 1.82600694444444E-06
TotalHours        : 4.38361666666667E-05
TotalMinutes      : 0.00263017
TotalSeconds      : 0.1578979
TotalMilliseconds : 157.8979

Use case 2: Pipe input to Measure-Command

Code:

10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_; $i++) {$i} }

Motivation: The motivation for using this example is to measure the time it takes to execute a script block on different input values. By piping input to Measure-Command, you can measure the execution time for each input value separately.

Explanation:

  • 10, 20, 50: These are the input values that will be piped to Measure-Command.
  • Measure-Command: This is the command that measures the time.
  • -Expression { for ($i=0; $i -lt $_; $i++) {$i} }: This is the script block that will be executed for each input value. It iterates from 0 to the input value and returns the value of each iteration.

Example output:

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 29
Ticks             : 295521
TotalDays         : 3.41826388888889E-07
TotalHours        : 8.203875E-06
TotalMinutes      : 0.000492231
TotalSeconds      : 0.0295521
TotalMilliseconds : 29.5521

Conclusion:

The Measure-Command command in PowerShell is a powerful tool for measuring the execution time of script blocks and cmdlets. By using this command, you can identify bottlenecks in your scripts or commands and optimize their performance. Measure-Command provides detailed information about the execution time, allowing you to make data-driven decisions to improve the efficiency of your PowerShell scripts.

Related Posts

How to use the command 'aplay' (with examples)

How to use the command 'aplay' (with examples)

The aplay command-line tool is used to play audio files using the ALSA soundcard driver.

Read More
How to use the command http-server-upload (with examples)

How to use the command http-server-upload (with examples)

This article aims to provide clear instructions on how to use the http-server-upload command, along with various use cases and examples.

Read More
How to use the command 'wtf' (with examples)

How to use the command 'wtf' (with examples)

The ‘wtf’ command is a part of the ‘bsdgames’ package in Debian.

Read More