Get Date - PowerShell


Overview

The Get-Date command in PowerShell allows you to retrieve the current date and time. It’s commonly used to obtain the system’s current timestamp, set timeouts, schedule tasks, and perform date and time calculations.

Syntax

Get-Date [-Date <DateTime>] [-Format <String>] [-TimeZone <String>] [[-UFormat <String>] [-Culture <String>]]

Parameters

| Parameter | Description |
|—|—|
| -Date | Specifies a specific date and time to be retrieved. |
| -Format | Customizes the output date and time format. |
| -TimeZone | Specifies the time zone for the retrieved date and time. |
| -UFormat | Customizes the output format using a Unicode standard date and time format string. |
| -Culture | Specifies the culture for the output date and time format. |

Options/Flags

| Flag | Description |
|—|—|
| None | No options or flags are available for Get-Date. |

Examples

Get the current date and time:

Get-Date

Get the date and time in a specific format:

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

Get the date and time in a specific time zone:

Get-Date -TimeZone "Eastern Standard Time"

Get the date and time using a Unicode standard date and time format:

Get-Date -UFormat "u"

Get the date and time for a specific culture:

Get-Date -Culture "es-ES"

Common Issues

Error Handling:

If an invalid -TimeZone or -Culture is specified, Get-Date will throw an error. Ensure proper values are used for these parameters.

Inaccurate Time:

If the system clock is incorrect, Get-Date will return an inaccurate date and time. Verify and adjust the system time if necessary.

Integration

Scripting:

Get-Date can be combined with other PowerShell commands to perform various tasks, such as:

  • Setting task schedules (e.g., Get-Date | Add-Time -Hours 2)
  • Calculating time differences (e.g., Get-Date -Date1 | Subtract-Date -Date2)
  • Converting time zones (e.g., Get-Date | Convert-TimeZone -DestinationTimeZone "Pacific Standard Time")
  • Add-Time: Adds a time interval to a specified date and time.
  • Convert-TimeZone: Converts a date and time to a different time zone.
  • Subtract-Date: Calculates the time difference between two dates and times.