This is done using the
New-Timespan cmdlet. It takes a start time and an end time and stores an object that
calculates the timespan in values from milliseconds all the way to days. For example, if
you wanted to time a script??™s execution, you could use something like this:
$starttime = Get-Date
??¦Do lots of stuff here??¦
$endtime = Get-Date
$timediff = New-TimeSpan $starttime $endtime
Write-Host $timediff.milliseconds + " milliseconds!"
Notice how we have used the milliseconds property of a time span to display the
number of milliseconds that elapsed. You have the following options:
?–? Days
?– Hours
?– Minutes
?– Seconds
?– Milliseconds
?– Ticks
?– Total Days
?– Total Hours
?– Total Minutes
?– Total Seconds
?–? Total Milliseconds
458 Microsoft Windows Server 2008 Administration
What if you wanted to know the time span between the current date and time and
January 1, 2001? Getting the current date and time is easy, but so is representing January 1,
2001, since you can use the Get-Date cmdlet to help you with this:
Get-Date -month 1 -day 1 -year 2001
To put it all together, you simply need to use New-TimeSpan to end up with this:
New-TimeSpan $(Get-Date) $(Get-Date -month 1 -day 1 -year 2001)
Notice how I had to surround the calls to Get-Date with parentheses. This is because
I??™m instructing Windows PowerShell to run the command within those parentheses first
and then use the value returned by it.
Pages:
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491