Measuring script execution time

Measuring script execution time:

PowerShell's Measure-Command cmdlet can be used to measure the execution time of cmdlets or script blocks. It has two parameters: Expression and InputObject. The cmdlet returns an object of type System.TimeSpan.

Scope and Object Modification:

Measure-Command runs the script block in the current scope, which can modify variables in the current scope. To avoid this, the script block can be executed in a separate context using the invocation operator and enclosed in a script block.

Output and Alternatives:

Measure-Command only returns a TimeSpan object, not the result from the script. The output can be saved in a variable or a custom function can be created to shape the output as desired.

Links:

Links to related methods such as InvokeWithContext and InvokeReturnAsIs are provided. A WindowsUtils module can be tested using these methods.