In this article we look at the Get-Date cmdlet in powershell

The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several .NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.

Get-Date uses the computer’s culture settings to determine how the output is formatted. To view your computer’s settings, use (Get-Culture).DateTimeFormat.

Parameters

-AsUTC

Converts the date value to the equivalent time in UTC.

This parameter was introduced in PowerShell 7.1.

-Date

Specifies a date and time. Time is optional and if not specified, returns 00:00:00.

Enter the date and time in a format that is standard for the system locale.

-Day

Specifies the day of the month that is displayed. Enter a value from 1 to 31.

If the specified value is greater than the number of days in a month, PowerShell adds the number of days to the month. For example, Get-Date -Month 2 -Day 31 displays March 3, not February 31.

-DisplayHint

Determines which elements of the date and time are displayed.

-Format

Displays the date and time in the Microsoft .NET Framework format indicated by the format specifier. The Format parameter outputs a String object.

For a list of available .NET format specifiers, see Custom date and time format strings.

When the Format parameter is used, Get-Date only gets the DateTime object’s properties necessary to display the date. As a result, some of the properties and methods of DateTime objects might not be available.

Starting in PowerShell 5.0, you can use the following additional formats as values for the Format parameter.

FileDate. A file or path-friendly representation of the current date in local time. The format is yyyyMMdd (case-sensitive, using a 4-digit year, 2-digit month, and 2-digit day). For example: 20190627.

FileDateUniversal. A file or path-friendly representation of the current date in universal time (UTC). The format is yyyyMMddZ (case-sensitive, using a 4-digit year, 2-digit month, 2-digit day, and the letter Z as the UTC indicator). For example: 20190627Z.

FileDateTime. A file or path-friendly representation of the current date and time in local time, in 24-hour format. The format is yyyyMMddTHHmmssffff (case-sensitive, using a 4-digit year, 2-digit month, 2-digit day, the letter T as a time separator, 2-digit hour, 2-digit minute, 2-digit second, and 4-digit millisecond). For example: 20190627T0840107271.

FileDateTimeUniversal. A file or path-friendly representation of the current date and time in universal time (UTC), in 24-hour format. The format is yyyyMMddTHHmmssffffZ (case-sensitive, using a 4-digit year, 2-digit month, 2-digit day, the letter T as a time separator, 2-digit hour, 2-digit minute, 2-digit second, 4-digit millisecond, and the letter Z as the UTC indicator). For example: 20190627T1540500718Z.

-Hour

Specifies the hour that is displayed. Enter a value from 0 to 23.

-Millisecond

Specifies the milliseconds in the date. Enter a value from 0 to 999.

This parameter was introduced in PowerShell 3.0.

-Minute

Specifies the minute that is displayed. Enter a value from 0 to 59.

-Month

Specifies the month that is displayed. Enter a value from 1 to 12.

-Second

Specifies the second that is displayed. Enter a value from 0 to 59.

-UFormat

Displays the date and time in UNIX format. The UFormat parameter outputs a string object.

UFormat specifiers are preceded by a percent sign (%), for example, %m, %d, and %Y. The Notes section contains a table of valid UFormat specifiers.

When the UFormat parameter is used, Get-Date only gets the DateTime object’s properties necessary to display the date. As a result, some of the properties and methods of DateTime objects might not be available.

-UnixTimeSeconds

Date and time represented in seconds since January 1, 1970, 0:00:00.

This parameter was introduced in PowerShell 7.1.

-Year

Specifies the year that is displayed. Enter a value from 1 to 9999.

Examples

get-date

(Get-Date).DateTime

Get-Date -DisplayHint Date

Get-Date -DisplayHint Time

Get-Date -Format “MM-dd-yyyy”

Get-Date -Format “MM/dd/yyyy”

Get-Date -Format “MM/dd/yyyy HH:mm:ss”

(Get-Date).AddDays(+3)

(Get-Date).AddDays(-2)

Video

Links

Microsoft documentation