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.
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) } }
More PowerShell Basics articles
- Working with dates in PowerShell revisited
- PowerShell regular expressions: Making string evaluation easier
- PowerShell concatenation: How to use this powerful feature
- What does a question mark mean in PowerShell commands?
- Using Group Policy settings to enforce PowerShell execution policies
More Quick Tips articles
- Fixing Azure Key Vault when moving to a different tenant
- Restore Azure Key Vault using just two PowerShell cmdlets
- This overlooked feature in Visual Studio Code can speed release time
- Enabling Front Door managed certificates in Azure: Status update
- How to quickly check the status of all your Azure services