System.TimeSpan represents the elapsed time between two time points which are represented by the System.DateTime struct. TimeSpan does not necessarily represent a continouos range of time (as in the case, for example, the value of TotalProcessorTime of the System.Diagnostic.Process class). Parse() is the default method of the [timespan), so, in PowerShell, a timespan can be initialized like so: [timespan] $two_minutes = '0:02' [timespan] $four_hours = '4:00' [timespan] $seven_seconds = '0:00:07' [timespan] $three_days = '3:00:00:00' [timespan] $negative_span = '-0:04'
ToString() can be used to format a time span. Literal text must be enclosed within single quotes, or escaped with backslashes: PS C:\> $ts = new-object TimeSpan 1, 23, 4, 56 PS C:\> $ts.ToString("d'd 'hh'h 'mm'm 'ss's'") 1d 23h 04m 56s PS C:\> $ts.ToString("d\d\ hh\h\ mm\m\ ss\s") 1d 23h 04m 56s
System.Diagnostics.Stopwatch is a class that allows to measure elapsed time. After measuring the elapsed period, it is returned by the Stopwatch's Elapsed property. new-timeSpan