Jun 212012
 

Check PowerShell Speed

To find out how much time a particular PowerShell cmdlet or command takes, here’s a handy little stopwatch that you can use:

functionTest {

  $codetext=$Args-join‘ ‘

  $codetext=$ExecutionContext.InvokeCommand.ExpandString($codetext)

 

  $code= [ScriptBlock]::Create($codetext)

 

  $timespan=Measure-Command$code

  “Your code took {0:0.000} seconds to run”-f$timespan.TotalSeconds

}

Now, to find out the performance of a particular command, try this:

PS> testGet-Service

Yourcodetook 0,004 secondstorun


PS
> testGet-WmiObjectWin32_Service

Yourcodetook 0,445 secondstorun

This can be especially useful if you have two cmdlets or parameters that apparently do the same. Performance difference can be tremendous, though:

PS> testdir$home-Include*.ps1-Recurse

Yourcodetook 5,741 secondstorun

PS> testdir$home-Filter*.ps1-Recurse
Yourcodetook 0,894 secondstorun

To test drive more complex commands, make sure you place them in quotes.

Leave a Reply