In this case, I would create an array of TimeSpan objects from the strings in $content and use these for the output.
IF $content is an array, use it straight away.
IF $content is a single string, you need to split it first with $content = $content -split '\r?\n'
# assuming $content is a single string, so we need to split into array:
$Content = @"
[Step1]::14:37:11
[Step2]::14:39:11
[Step3]::14:42:14
[Step4]::14:45:09
[Step5]::14:57:12
"@ -split '\r?\n'
Next loop through this string array and remove empty lines if there are any:
$times = $content | Where-Object { $_ -match '\S' } | ForEach-Object {
$h, $m, $s = ($_.Trim() -replace '.*(\d{2}:\d{2}:\d{2})$', '$1') -split ':'
[TimeSpan]::new([int]$h, [int]$m, [int]$s)
}
for ($i = 1; $i -lt $times.Count; $i++) {
$timeDiff = $times[$i] - $times[$i - 1]
# output the times taken for each step
"Step $($i + 1) took $($timeDiff.ToString()) to complete."
}
Output:
Step 2 took 00:02:00 to complete.
Step 3 took 00:03:03 to complete.
Step 4 took 00:02:55 to complete.
Step 5 took 00:12:03 to complete.
Regex details for -replace:
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 1
\d Match a single digit 0..9
{2} Exactly 2 times
: Match the character “:” literally
\d Match a single digit 0..9
{2} Exactly 2 times
: Match the character “:” literally
\d Match a single digit 0..9
{2} Exactly 2 times
)
$ Assert position at the end of the string (or before the line break at the end of the string, if any)
As per your new requirements in the comment, here's the somewhat altered code:
$Content = @"
[Step1]::14:37:11
[Step2]::14:39:11
[Test3]::14:42:14
[Example4]::14:45:14
"@ -split '\r?\n'
$times = $content | Where-Object { $_ -match '\S' } | ForEach-Object {
# capture the action like '[Example4]' and the timestamp part
$action, $processTime = $_.Trim() -split '::'
# split the timestamp into Hours, Minutes, Seconds
$h, $m, $s = ($processTime -replace '.*(\d{2}:\d{2}:\d{2})$', '$1') -split ':'
# output an object with two properties, the action name and a TimeSpan
[PsCustomObject]@{
'Action' = $action
'Time' = [TimeSpan]::new([int]$h, [int]$m, [int]$s)
}
}
# loop through the returned array, get the time difference between steps
for ($i = 1; $i -lt $times.Count; $i++) {
$timeDiff = $times[$i].Time - $times[$i - 1].Time
# output the times taken for each step, in this new requirement rounded to show minutes only
'{0} took {1} minutes to complete' -f $times[$i].Action, [math]::Round($timeDiff.TotalMinutes, 0)
}
Output:
[Step2] took 2 minutes to complete
[Test3] took 3 minutes to complete
[Example4] took 3 minutes to complete