{"id":74193,"date":"2015-01-19T00:01:00","date_gmt":"2015-01-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/19\/working-with-dates-in-powershell\/"},"modified":"2019-02-18T10:35:54","modified_gmt":"2019-02-18T17:35:54","slug":"working-with-dates-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/working-with-dates-in-powershell\/","title":{"rendered":"Working with Dates in PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about the fundamentals of working with dates in Windows PowerShell.<\/span>\nMicrosoft 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.\nBut you know, I am probably not the only one who dislikes working with dates and times&mdash;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&mdash;if I dislike all of the standard ways of formatting the dates and times, I can always invent another one.\nThe 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 <b>System.DateTime<\/b> object. MSDN documents lots of methods and properties for this object (for more information, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.datetime(v=vs.110).aspx\" target=\"_blank\">DateTime Structure<\/a>). This is a good thing. In fact, it is a very good thing because it makes working with dates and times a lot easier.\nA 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.\nA time is made up of hour, minute, and second. But not only that&mdash;it is also made up of milliseconds and ticks. These are also properties of the System.DateTime object.\nThe nice thing about having a <b>DateTime<\/b> 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&mdash;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.\nNot 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).\nDates and times are everywhere. They are present as the system time on a computer&mdash;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.\nThe 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 <b>DateTime<\/b> object.<\/p>\n<h2>Obtaining a DateTime object<\/h2>\n<p>This is really easy. All I need to do is to type <b>Get-Date<\/b>, and voila! I have an instance of the <b>DateTime<\/b> 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.:\n&nbsp; &nbsp;PS C:&gt; Get-Date<\/p>\n<p style=\"margin-left:30px\">Thursday, January 15, 2015 7:41:18 PM\n<b>&nbsp; &nbsp;Note&nbsp;<\/b> One of the really great things about Windows PowerShell is that it always uses the local cultural information <br \/>&nbsp; &nbsp;to control the way the <b>Get-Date<\/b> cmdlet returns the default information. Changing cultural settings changes the way <br \/>&nbsp; &nbsp;the date and time display.\nIf I want to see the values of all of the properties of the System.DateTime object, I can pipe the results of <b>Get-Date<\/b> to the <b>Format-List<\/b> cmdlet. I select all of the properties (via a wild card character) and use the <b>&ndash;Force <\/b>parameter to tell it to display any hidden properties. This command and its output are shown here:\n&nbsp; &nbsp;PS C:&gt; Get-Date | format-list * -Force<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p style=\"margin-left:30px\">DisplayHint : DateTime<\/p>\n<p style=\"margin-left:30px\">DateTime&nbsp;&nbsp;&nbsp; : Thursday, January 15, 2015 7:44:33 PM<\/p>\n<p style=\"margin-left:30px\">Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/15\/2015 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">Day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 15<\/p>\n<p style=\"margin-left:30px\">DayOfWeek&nbsp;&nbsp; : Thursday<\/p>\n<p style=\"margin-left:30px\">DayOfYear&nbsp;&nbsp; : 15<\/p>\n<p style=\"margin-left:30px\">Hour&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 19<\/p>\n<p style=\"margin-left:30px\">Kind&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Local<\/p>\n<p style=\"margin-left:30px\">Millisecond : 196<\/p>\n<p style=\"margin-left:30px\">Minute&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 44<\/p>\n<p style=\"margin-left:30px\">Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"margin-left:30px\">Second&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 33<\/p>\n<p style=\"margin-left:30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 635569478731960307<\/p>\n<p style=\"margin-left:30px\">TimeOfDay&nbsp;&nbsp; : 19:44:33.1960307<\/p>\n<p style=\"margin-left:30px\">Year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2015\nFrom this list of properties, I can easily choose any one property to display. I can do this by using the <b>Select-Object<\/b> cmdlet. To do this, I pipe the results from <b>Get-Date<\/b> by using the pipe character( <b>|<\/b> ):\n&nbsp; &nbsp;PS C:&gt; Get-date | Select-Object date<\/p>\n<p style=\"margin-left:30px\">Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">1\/15\/2015 12:00:00 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\nNotice 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 <b>Get-Date<\/b> cmdlet. By using dotted notation, you will return only the date property:\n&nbsp; &nbsp;PS C:&gt; (Get-Date).Date<\/p>\n<p style=\"margin-left:30px\"><span style=\"font-size:12px\">Thursday, January 15, 2015 12:00:00 AM<\/span>\nOnce 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 <b>&ndash;DisplayHint<\/b> parameter and choose <b>Date<\/b>:\n&nbsp; &nbsp;PS C:&gt; get-date -DisplayHint Date<\/p>\n<p style=\"margin-left:30px\"><span style=\"font-size:12px\">Thursday, January 15, 2015<\/span>\nThat 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.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,3,4,45],"class_list":["post-74193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}