1

Trying to take the following output (listed below) from a PS script and create another output file stating the time it took for each step to complete. Time is in format of day:hr:min:sec

Current output:
[Cpu 1] at [Step 1] completed at 120:15:12:15
[Cpu 1] at [Step 2] completed at 120:15:17:18
[Cpu 1] at [Step 3] completed at 120:16:37:13

Since Step 2 finished at 120:15:17:18, the total time would be found by 120:15:17:18 - 120:15:12:15. Step 3 finished at 120:16:37:13, so the total time would be found by 120:16:37:13 - 120:15:17:18.

Desired output: 
'Step 2 took xx:xx:xx:xx to complete'
'Step 3 took xx:xx:xx:xx to complete'
3
  • what have you tried so far? how does your script look and what are the errors you are getting? Commented May 13, 2020 at 19:48
  • what are the time units? the ones you show are not standard ones ... [grin] Commented May 13, 2020 at 20:24
  • Show your code that creates this output. For the most part, you should be able to leverage the Measure-Command cmdlet for what you are after. Commented May 13, 2020 at 22:19

1 Answer 1

2

Use ...

Measure-Command

# Examples
Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }

Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}

10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_ i++) {$i} }

10, 20, 50 | Measure-Command -Expression {for ($i=0; $i -lt $_; $i++) {$i}; "$($_)" | Out-Default }

... and use the TotalMilliseconds property as your result.

Or use a stopwatch in your code block to get the elapsed time.

# Instantiate and start a new stopwatch
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

Stopwatch Class

Stopwatch.StartNew Method

PowerShell - Measure Script execution time

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.