Prof. Powershell

All the Time in World

That New-TimeSpan cmdlet watches the clock for you, so you can attend to important tasks in a timely manner.

Whether we like it or not, time plays a critical role in the life of a Windows administrator. Is it time for a backup? How old are those files? How old is that administrator password? Is it time to go home? I don't know if I can help with the last one, but I will show you a PowerShell cmdlet that might come in handy for some of the other tasks: New-TimeSpan.

The New-TimeSpan cmdlet creates a special type of object, called, naturally, a TimeSpan. There a two ways to use this cmdlet. First, you can create a TimeSpan object of a specific length, like this:

PS C:\> $timespan = new-timespan -days 1 -hour 1 -minute 30 -seconds 15
PS C:\> $timespan

Days              : 1
Hours             : 1
Minutes           : 30
Seconds           : 15
Milliseconds      : 0
Ticks             : 918150000000
TotalDays         : 1.06267361111111
TotalHours        : 25.5041666666667
TotalMinutes      : 1530.25
TotalSeconds      : 91815
TotalMilliseconds : 91815000

What I find useful, though, is the automatically calculation for properties like total minutes:

PS C:\> $timespan.totalminutes
1530.25

Personally, I've always found the other New-Timespan syntax more valuable. Tell the cmdlet to create a timespan object calculated from a start date to an end date:

PS C:\> $ts=new-timespan (get-date) 6:00:00PM
PS C:\> write-host "You have $ts of work left to do."
You have -09:15:26.3770000 of work left to do.

The Timespan object is the difference between the current date and time and 6:00 PM. All I've done is indicate how much longer the work day is. Depressing isn't it?

But here's one more way you might use this. Consider a folder of user files. The Get-ChildItem cmdlet will show you properties, such as when the file was created and when it as last modified. Wouldn't it be nice to get a simple value in days? Here's how:

dir c:\files | select FullName,CreationTime,@{name="CreationAge";
expression={[int](New-TimeSpan ($_.CreationTime) (Get-Date)).TotalDays }},
LastWriteTime,@{name="ModificationAge";
expression={[int](New-TimeSpan ($_.LastWriteTime) (Get-Date)).TotalDays }}

I use the Select-Object cmdlet to create a custom property like CreationAge. The value for this property is the TotalDays property of a time span object, which is the difference between the object's CreationTime and the current date. I cast it as an integer to round the value:

expression={[int](New-TimeSpan ($_.CreationTime) (Get-Date)).TotalDays }}

When I run this expression, I get output like this for each file:

FullName : C:\files\users.txt
CreationTime : 11/9/2007 9:13:20 AM
CreationAge : 171
LastWriteTime : 1/2/2008 10:36:32 AM
ModificationAge : 117

FullName : C:\files\weather.xml
CreationTime : 11/21/2007 3:37:22 PM
CreationAge : 159
LastWriteTime : 1/17/2008 12:10:10 PM
ModificationAge : 102

I could even take this a step further and add this:

| sort ModificationAge | export-csv c:\agereport.csv

to the end of the expression. This will sort the output by my custom ModificationAge property and send the results to a CSV file.

The New-TimeSpan cmdlet won't extend your lunch break, but it might make some of your other administrative tasks a little less time-consuming.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.