How To Tell How Long A PowerShell Command Takes

In this video, Jason is going to show us how to measure how fast or slow our code really is. Using the methods provided in this video, you will be able to determine any bottlenecks in your scripts that could be costing you valuable seconds. Watch this video, measure your code, optimize your code, tell your friends how fast your code now runs.

In this step. We're going to focus on measuring how long it takes for a powershell command to run let's get started. Looking at our first example. We're going to use the native PowerShell Commandlet Measure Command Measure Command is a great and simple command, let to find out how quickly a command takes simply provide your code to the expression parameter in the form of a script block and execute executing our command. We see that it took 7 milliseconds to run the get service commandlet. Looking at our second example. We will use datetime objects to determine how long a command takes when using datetime objects. We must first set a starting time variable at the top of our code and the ending time variable at the end with our code. In between doing some basic math between the ending and start time we were able to determine how long are code took to execute. Executing our code were able determined that it took 5 milliseconds to run the get service commandet again. In our last example. We're going to use the class. The stop watch class is very accurate in operates just like a physical stop watch once it is started. It keeps the track of the time and can be stopped at any time as well. To start the stop watch you need to execute the start new method. Let's go ahead and start our stop watch. After the stop watch has started the elapsed property is available to us to determine how much time has passed. Here we are going to look at the elapsed milliseconds. The is running property, let's you know if the stop watch is running. Here we can see the stop watch is still running. Did you stop the stop watch you simply execute these stop method? And now we have stopped the stopwatch. Finally, to see the total amount of time Passed, you can look at the elapsed property with the Time Increment of your choice. Here we can see that the total milliseconds was around 38,000 since we started the stop watch. That covers how to measure how long a powerShell Command takes to run. Thank you for watching.