PowerShell for Windows Admins

Sep 14 2018   9:10AM GMT

Get-Date – DisplayHint

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: