PowerShell Quick Tip: Track the time it takes for your scripts to run

When running PowerShell scripts it is always good to measure the time that your script is taking to complete any given task. I wrote a small function that you can use on your scripts at the start and end to accomplish this goal.

The function is simple, and we need to pass a variable. If the variable is empty, then it will receive the current time (Item 1). When we execute it a second time, it will compare the time difference to the variable (Item 2). The variable after the second run (Item 3) contains the time that it took in minutes, days, hours, and seconds.

PowerShell script time

The function to track the time it takes for your PowerShell script to run is comprised of two lines, as follows.

Function TrackTime($Time){
If (!($Time)) { Return Get-Date } Else {
Return ((get-date) - $Time)
}
}

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top