Home » How to Get Date and Time with UFormat in PowerShell

How to Get Date and Time with UFormat in PowerShell

You can use the Get-Date cmdlet in PowerShell with -UFormat to get the date in a Unix date format. The -UFormat parameter is used to specify a Unix-style date and time format string.

The following method shows how to do it with syntax.

Method 1: Get the Date in a Specific Format

Get-Date -UFormat "%Y-%m-%d %H:%M:%S"

This example returns the current date and time in a Unix-style date and time format.

The following example shows how to use this method.

Get Date with UFormat Parameter in PowerShell

The following example shows how to use the Get-Date cmdlet with -UFormat to get the current date and time in a specific format.

# Get the current date and time in the format
$currentDateTime = Get-Date -UFormat "%Y-%m-%d %H:%M:%S"

#display the current date time
Write-Output $currentDateTime

Output:

(base) PS C:> Get-Date -UFormat "%Y-%m-%d %H:%M:%S"

2024-05-02 23:00:12

In this example, the %Y represents the year in a four-digit format, %m represents the month (0-12), %d represents the day of the month (01-31), %H represents the hour (24-hour format), %M represents the minute (00-59) and %S represents the second (00-59).

In the Unix-style date formatting, each format specifier is preceded by a percent sign (%) representing a specific date or time element.

Conclusion

I hope the above article on getting the date with the -UFormat parameter in the PowerShell Get-Date cmdlet is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.