PowerShell Get-Date

The Get-Date cmdlet in PowerShell displays the current date and time on the PowerShell console. This cmdlet gets a DateTime object.

We can also use the Get-Date to generate and send the date and time character string to the other cmdlets or programs.

Syntax

Parameters

Following are the parameters used in Get-Date cmdlet:

-Date

This parameter allows you to specify a particular date and time in the cmdlet. By default, the get-date cmdlet returns the system date and time. In this parameter, time is optional if you do not specify it in the command, then the command returns 00:00:00.

-Year

This parameter is used to specify the year. Enter the value of a Year from 1 to 9999, which is followed by this parameter.

-Month

This parameter is used to specify the month of the year, which is displayed. Enter the value of a month from 1 to 12, which is followed by this parameter.

-Day

This parameter is used to specify the day of the month, which is displayed. Enter the value of a day from 1 to 31, which is followed by this parameter.

If the specified value of the day in the cmdlet is greater than the number of days in the month, then the PowerShell adds the number of days to the month.

-Hour

This parameter is used to specify the hour. Enter the value of the hour from 0 to 23, which is followed by this parameter.

-Minute

This parameter is used to specify the minute. Enter the value of a minute from 0 to 59, which is followed by this parameter.

-Second

This parameter is used to specify the second. Enter the value of a second from 0 to 59, which is followed by this parameter.

-Millisecond

-Millisecond parameter was introduced in PowerShell version 3.0 and is used to specify the milliseconds in the date. Enter the value of millisecond from 0 to 999.

-DisplayHint

This parameter determines which value of date and time are displayed on the console.

-Format

This parameter shows the date and time in the format of the Microsoft .NET framework.

With the starting of PowerShell version 5.0, for this parameter, we can use the following formats as its values:

  • FileDate
  • FileDateUniversal
  • FileDateTime
  • FileDateTimeUniversal

-UFormat

This parameter is used to display the date and time in the format of UNIX. This parameter outputs an object of a string. The specifiers of this parameter are preceded by a percent sign (%).

Examples

Example 1: Display the current date and time

PowerShell Get-Date

In this example, Get-Date cmdlet shows the current date and time of the system.

Example 2: Display the current date

PowerShell Get-Date

In this example, the cmdlet Get-Date uses the -DisplayHint parameter with the value argument to get the date only.

Example 3: Display the date and time with a .NET format specifier

PowerShell Get-Date

In this example, the Get-Date cmdlet uses the -Format parameter to display the current date and time with a .NET format specifier.

In this example, we use the following format specifiers:

  • dddd: Day of the Week
  • mm: Month of the Year
  • dd: Day of the Month
  • yyyy: Year in 4-digit format
  • HH:mm: Time is in the 24-hour format

Example 4: Display only a month of the year

PowerShell Get-Date

In this example, the Get-Date cmdlet is used with the property month.


Next TopicIf Statement