PowerShell Get-Date UTC

Recently, one of my team members was trying to get today’s time in UTC format. I suggested the right approaches. In this tutorial, I will explain how to use Get-Date with UTC in PowerShell.

To get the current date and time in UTC using PowerShell, you can utilize the Get-Date cmdlet with the -AsUTC parameter. For instance, executing $utcDate = Get-Date -AsUTC retrieves the current UTC date and time, which you can then output or use in further scripting.

PowerShell Get-Date UTC

Get-Date cmdlet in PowerShell allows you to retrieve the current date and time. By default, it provides the local date and time, but you can easily convert it to UTC (Coordinated Universal Time) or any other time zone.

The basic syntax for Get-Date is:

Get-Date

To get the current date and time in UTC in PowerShell, you can use the -AsUTC parameter:

Get-Date -AsUTC

You can see the output in the screenshot below:

powershell get-date utc

The Get-Date cmdlet is useful for various tasks, such as logging, timestamping, and scheduling. Converting to UTC when working across different time zones ensures consistency and avoids confusion.

Check out Compare Dates Without Time in PowerShell

Examples of Using Get-Date with UTC

Let me show you some practical examples to see how Get-Date can be used with UTC in PowerShell.

Example 1: Get Current Date and Time in UTC

To get the current date and time in UTC, simply use the -AsUTC parameter:

$utcDate = Get-Date -AsUTC
Write-Output "Current UTC Date and Time: $utcDate"

Here is the output in the screenshot below:

Examples of Using Get-Date with UTC

Example 2: Format the UTC Date and Time

You can format the UTC date and time to suit your needs using the -Format parameter:

$utcFormattedDate = Get-Date -AsUTC -Format "yyyy-MM-ddTHH:mm:ssZ"
Write-Output "Formatted UTC Date and Time: $utcFormattedDate"

Example 3: Convert Local Time to UTC

If you have a local date and time and need to convert it to UTC, you can use the ToUniversalTime() method:

$localDate = Get-Date
$utcDate = $localDate.ToUniversalTime()
Write-Output "Local Date and Time: $localDate"
Write-Output "Converted UTC Date and Time: $utcDate"

Check out Check If Date Is Older Than 30 Days in PowerShell

Other Methods Related to PowerShell Get-Date UTC

In addition to the basic usage of Get-Date, there are other methods and parameters you can use to work with dates and times in PowerShell.

Method 1: Add and Subtract Time

You can add or subtract time from the current date using the AddDays(), AddHours(), AddMinutes(), etc., methods. Here’s an example of adding 5 days to the current UTC date:

$utcDate = Get-Date -AsUTC
$futureDate = $utcDate.AddDays(5)
Write-Output "Current UTC Date: $utcDate"
Write-Output "UTC Date 5 Days Later: $futureDate"

Here is the output in the screenshot below:

PowerShell Get-Date UTC Examples

Method 2: Get Specific Date Components

You can extract specific components of the date, such as the year, month, day, hour, minute, and second:

$utcDate = Get-Date -AsUTC
$year = $utcDate.Year
$month = $utcDate.Month
$day = $utcDate.Day
$hour = $utcDate.Hour
$minute = $utcDate.Minute
$second = $utcDate.Second

Write-Output "Year: $year"
Write-Output "Month: $month"
Write-Output "Day: $day"
Write-Output "Hour: $hour"
Write-Output "Minute: $minute"
Write-Output "Second: $second"

Read Get the Last Business Day of the Month Using PowerShell

Method 3: Parsing Dates

You can parse a string into a date object using the [datetime]::ParseExact() method. This is useful when you need to convert a string representation of a date into a DateTime object:

$dateString = "2023-10-01T15:30:00Z"
$utcDate = [datetime]::ParseExact($dateString, "yyyy-MM-ddTHH:mm:ssZ", $null)
Write-Output "Parsed UTC Date: $utcDate"

Conclusion

Using Get-Date with UTC in PowerShell is a powerful way to handle dates and times, especially when working across different time zones. Whether you need the current UTC date, want to format it, or convert local time to UTC, Get-Date provides the UTC parameter.

I hope this tutorial has helped you understand how to use Get-Date with UTC in PowerShell. Still have questions? Do leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.