Working with Dates in PowerShell


Summary: Microsoft Scripting Guy, Ed Wilson, talks about the fundamentals of working with dates in Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. For as long as I can remember, I have pretty much always hated working with dates and times. Back on my Osborne One, it was no fun. The Atari did not make it any better; neither did my first IBM-compatible PCs.

But you know, I am probably not the only one who dislikes working with dates and times—that must be one reasons there are so many ways of formatting the silly things. I mean, there must be nearly a hundred different ways (I am not exaggerating too much). And not only that—if I dislike all of the standard ways of formatting the dates and times, I can always invent another one.

The problem is that a date and a time are composites; that is, they are objects. In Windows PowerShell, there is a standard .NET Framework object that is used: the System.DateTime object. MSDN documents lots of methods and properties for this object (for more information, see DateTime Structure). This is a good thing. In fact, it is a very good thing because it makes working with dates and times a lot easier.

A date is made up of not only year, month, and day. It also includes a day of the week and even a day of the year. These are all properties of the System.DateTime object.

A time is made up of hour, minute, and second. But not only that—it is also made up of milliseconds and ticks. These are also properties of the System.DateTime object.

The nice thing about having a DateTime object is that I can access each part of the date or the time as a single property of the object. In the old days (and one reason I hated dates and times so much), the date or the time would return as a string—or even as a series of strings. I then had to parse the string to find what the hour was, or I had to concatenate a string to express a complete date or time.

Not only that. The math was not standard either. I could not simply add several times together because I might end up with something really weird. The same thing held true for subtracting times from one another (such as when I have two time stamps and I want to see how long a process ran).

Dates and times are everywhere. They are present as the system time on a computer—and that controls things such as logging to the event log and timestamps for files, software installation, user management, auditing, and other security related tasks. From a basic reporting standpoint, I need to be able to work with dates and times whenever I work with any of those things.

The good news is that with the System.DateTime object, working with dates and times is a breeze. The first step, however, is to obtain an instance of a DateTime object.

Obtaining a DateTime object

This is really easy. All I need to do is to type Get-Date, and voila! I have an instance of the DateTime object. As shown here, it displays basic information to the console: the day of the week, the month, day, year, hour, minute, second, and A.M. or P.M.:

   PS C:> Get-Date

Thursday, January 15, 2015 7:41:18 PM

   Note  One of the really great things about Windows PowerShell is that it always uses the local cultural information
   to control the way the Get-Date cmdlet returns the default information. Changing cultural settings changes the way
   the date and time display.

If I want to see the values of all of the properties of the System.DateTime object, I can pipe the results of Get-Date to the Format-List cmdlet. I select all of the properties (via a wild card character) and use the –Force parameter to tell it to display any hidden properties. This command and its output are shown here:

   PS C:> Get-Date | format-list * -Force 

DisplayHint : DateTime

DateTime    : Thursday, January 15, 2015 7:44:33 PM

Date        : 1/15/2015 12:00:00 AM

Day         : 15

DayOfWeek   : Thursday

DayOfYear   : 15

Hour        : 19

Kind        : Local

Millisecond : 196

Minute      : 44

Month       : 1

Second      : 33

Ticks       : 635569478731960307

TimeOfDay   : 19:44:33.1960307

Year        : 2015

From this list of properties, I can easily choose any one property to display. I can do this by using the Select-Object cmdlet. To do this, I pipe the results from Get-Date by using the pipe character( | ):

   PS C:> Get-date | Select-Object date

Date                                                                                 

----                                                                                  

1/15/2015 12:00:00 AM     

Notice that the output not only contains the date, but it also displays the time (midnight). Another way of obtaining the date is to use parenthesis around the Get-Date cmdlet. By using dotted notation, you will return only the date property:

   PS C:> (Get-Date).Date

Thursday, January 15, 2015 12:00:00 AM

Once again, it returns both the date and the time of midnight. The best way (and indeed the easiest way) to return only the date is to use the –DisplayHint parameter and choose Date:

   PS C:> get-date -DisplayHint Date

Thursday, January 15, 2015

That is all there is to working with dates in Windows PowerShell. Dates and Times Week will continue tomorrow when I will talk about creating a specific date.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (6)

  1. Chen V says:

    Excellent and interesting is force parameter ??

  2. BASATI says:

    Good One. Very helpful as I am creating an event log for my powershell winform application………

  3. So what would be the input if i wanted the us-format in a european local. Example: 15-1-2015 (EU style) but i want this formatted as 1-15-2015?

  4. Anonymous says:

    Is there a way to obtain the week of year?

  5. Ed (not Wilson) says:

    At least with 2015-01-15, you know the date. 🙂

  6. Ed (not Wilson) says:

    @ynking : Take the number of days in the year so far. Divide by 7. Take the full number and add 1. So 15/7 = 2.14. Answer is 2+1=3.

Skip to main content