Get-Date –DisplayHint

On the surface Get-Date is a simple cmdlet that’s been around since PowerShell v1. However, it has some interesting quirks. In this post I’ll show how Get-Date  –DisplayHint works.

By default Get-Date returns the data and time

PS>  Get-Date

13 September 2018 16:09:34

The DisplayHint parameter ( available in PowerShell v5.1, 6.0 and 6.1) modifies the data shown

PS>  Get-Date -DisplayHint Date

13 September 2018

PS>  Get-Date -DisplayHint DateTime

13 September 2018 16:11:18

PS>  Get-Date -DisplayHint Time

16:11:25

You might think that all –DisplayHint does is just force the display to be date, time or datetime and you’ve lost the rest of the data but in reality all that’s happening is that the default display of your object is constrained by the value of the DisplayHint property:

PS> Get-Date | Get-Member -Name DisplayHint

    TypeName: System.DateTime

Name        MemberType   Definition
—-        ———-   ———-
DisplayHint NoteProperty DisplayHintType DisplayHint=DateTime

PS>  Get-Date -DisplayHint Date | Get-Member -Name DisplayHint

    TypeName: System.DateTime

Name        MemberType   Definition
—-        ———-   ———-
DisplayHint NoteProperty DisplayHintType DisplayHint=Date

PS>  Get-Date -DisplayHint Time | Get-Member -Name DisplayHint

    TypeName: System.DateTime

Name        MemberType   Definition
—-        ———-   ———-
DisplayHint NoteProperty DisplayHintType DisplayHint=Time

Note that DateTime is the default setting if –DisplayHint isn’t used.

PS>  $t = Get-Date -DisplayHint Time
PS>  $t

16:16:59

But you can still access the other properties of the datetime object returned by get-date

PS>  $t.Date

13 September 2018 00:00:00

PS>  $t.Year
2018
PS>  $t.Hour
16

If you just need to see the date or time –DisplayHint is a good place to start

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 )

Connecting to %s