PowerShell get-date [With examples]

In this PowerShell tutorial, I will explain everything about the get-date PowerShell cmdlets with examples. Then I will cover the PowerShell get-date -uformat with examples.

PowerShell get-date

The Get-Date cmdlet in PowerShell is used to get the current date and time. It can also be used to format the output, create a datetime object with a specific date and time, and perform date arithmetic. When you run this command, PowerShell displays the current date and time, including details such as the day of the week, month, day, year, and the time down to the second.

Now, let us check a few usages of Get-Date with examples in PowerShell.

The basic syntax of the Get-Date cmdlet is straightforward:


When you run this command, you’ll see an output similar to this:

03 January 2024 23:20:53

After I executed the PowerShell script using Visual Studio Code, you can see the output in the screenshot below.

powershell get-date

One of the most useful features of Get-Date is its ability to format the date and time output. You can use the -Format parameter to specify the format of the date and time.

For example, to display the date in a “Month/Day/Year” format, you can write the script like below:

Get-Date -Format "MM/dd/yyyy"

Once you execute the code using VS code, you will get the output in the screenshot below:

Get-Date -Format "MM/dd/yyyy"

There are numerous format specifiers available, such as:

  • MM: Month number
  • dd: Day of the month
  • yyyy: Year
  • hh: Hour
  • mm: Minute
  • ss: Second
See also  Register-PnPManagementShellAccess is not recognized

The Get-Date cmdlet allows you to format the date and time using the -Format parameter. You can use standard .NET date and time format specifiers.

Example of a custom format:

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

PowerShell get-date -uformat

The -UFormat parameter in the Get-Date cmdlet allows you to format the date and time output using Unix-like date formatting strings. This is particularly useful when you need to create timestamps for filenames, logs, or when you need to format the date in a specific way for display or processing purposes.

How to Use -UFormat in PowerShell get-date

Using the -UFormat parameter is straightforward. You need to provide a format string that specifies how you want the date and time to be displayed. Here’s the basic syntax:

Get-Date -UFormat "<format-string>"

The format string consists of special characters representing different date and time components. For example:

  • %Y – Year as a four-digit number (e.g., 2023)
  • %m – Month as a two-digit number (e.g., 04)
  • %d – Day of the month as a two-digit number (e.g., 12)
  • %H – Hour in 24-hour format (e.g., 14 for 2 PM)
  • %M – Minute as a two-digit number (e.g., 30)
  • %S – Second as a two-digit number (e.g., 45)

Let’s look at some examples:

# Get the current date in YYYY-MM-DD format
Get-Date -UFormat "%Y-%m-%d"

# Get the current time in HH:MM:SS format
Get-Date -UFormat "%H:%M:%S"

# Get the current date and time with custom format
Get-Date -UFormat "%Y-%m-%d %H:%M:%S"

Once you execute the PowerShell script using VS code, you can see the output like below:

powershell get-date -uformat

Practical Examples of get-date -uformat PowerShell

Let us check a few practical examples of get-date -uformat in PowerShell.

  1. Logging: If you’re writing a script that logs events, you might want to prepend the log entries with a timestamp:
$timestamp = Get-Date -UFormat "[%Y-%m-%d %H:%M:%S]"
$logEntry = "$timestamp An event has occurred."
Add-Content -Path "C:\logs\eventlog.txt" -Value $logEntry
  1. Filenames: When creating files with a date-time stamp in their name, you can use -UFormat:
$filename = "backup_" + (Get-Date -UFormat "%Y%m%d%H%M%S") + ".zip"
# This will create a filename like "backup_20230412143045.zip"
  1. Data Processing: When processing data, you might need to convert date formats. For example, converting a date from one format to another for CSV output:
$dateForCsv = Get-Date "2024-04-12 14:30:45" -UFormat "%d/%m/%Y %H:%M:%S"
# Converts to "12/04/2024 14:30:45"


In this PowerShell tutorial, I have explained how to use the get-date cmdlet in PowerShell, especially how to work with PowerShell get-date -uformat with a few real examples.

See also  PowerShell Functions - Complete Guide

You may also like:

  • >