Get-Date format

A common question revolves around Get-Date format. In other words how can you format the output of Get-Date. 

Standard output is

PS> Get-Date

27 February 2018 16:02:13

You can use –DisplayHint to control what’s displayed

PS> Get-Date -DisplayHint Date

27 February 2018

PS> Get-Date -DisplayHint Time

16:03:09

PS> Get-Date -DisplayHint DateTime

27 February 2018 16:03:17

Which is fine if you just want the date or the time

You’ve also got some methods on the DateTime object that can help

PS> (Get-Date).ToShortDateString()
27/02/2018
PS> (Get-Date).ToShortTimeString()
16:05
PS> (Get-Date).ToLongDateString()
27 February 2018
PS> (Get-Date).ToLongTimeString()
16:05:38
PS> (Get-Date).ToFileTime()
131642211467063624
PS> (Get-Date).ToFileTimeUtc()
131642211545205423
PS> (Get-Date).ToUniversalTime()

27 February 2018 16:07:01

Universal Time is more properly known as Greenwich Mean Time

If you want a bit more control you can use the –Format parameter. A description of the format specifiers is available at https://msdn.microsoft.com/en-GB/Library/system.globalization.datetimeformatinfo(VS.85).aspx

(Get-Date).GetDateTimeFormats()

will give a very long list of the possible formats. Unfortunately it just displays the results not the format specifier.

There are some preset formats – for example:

PS> Get-Date -Format g
27/02/2018 16:14
PS> Get-Date -Format r
Tue, 27 Feb 2018 16:14:58 GMT
PS> Get-Date -Format s
2018-02-27T16:15:03

Or you can customise the format

PS> Get-Date -Format “ddMMyyyyhhmmss”
27022018041748
PS> Get-Date -Format “dd/MM/yyyy hh:mm:ss”
27/02/2018 04:18:03
PS> Get-Date -Format “dd/MM/yyyy HH:mm:ss”
27/02/2018 16:18:08

Read the article at the link for the full list of options. NOTE – the options ARE case sensitive

Alternatively, you could use the –Uformat parameter to use Unix formatting. This is explained in the NOTES section of the Get-Date help file.

Some examples

PS> Get-Date -UFormat “%d/%m/%Y %r”
27/02/2018 04:23:25 PM
PS> Get-Date -UFormat “%d/%m/%Y %R”
27/02/2018 16:23

Between the display hints, the methods, –Format and –UFormat you should be able to get the date into the format you need.

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s