PowerShell Get-Date

While planning for a PowerShell webinar, I prepared for PowerShell Get-Date. In this tutorial, I will explain the PowerShell Get-Date cmdlet with examples.

The Get-Date cmdlet in PowerShell is used to retrieve the current date and time of the system. Simply running Get-Date without any parameters will return the current date and time in the default format. This cmdlet is incredibly useful for tasks that require date and time information, such as logging events or scheduling tasks.

What is PowerShell Get-Date?

The Get-Date cmdlet in PowerShell is used to retrieve the current date and time of the system. It can also be used to format dates, perform date arithmetic, and convert date formats. This cmdlet is useful for scripting and automation tasks that require date and time information.

Syntax

The basic syntax of the Get-Date cmdlet is:

Get-Date

However, Get-Date comes with several parameters that allow for more advanced usage:

Get-Date [-Date] <DateTime> [-DisplayHint {Date | Time | DateTime}] [-Format <String>] [-UFormat <String>] [<CommonParameters>]

Basic Usage

Let’s start with the simplest form of Get-Date:

Get-Date

This command will return the current date and time of your system.

I executed the above PowerShell cmdlet, and you can see the output in the screenshot below:

powershell get-date

Format Dates

One of the powerful features of Get-Date is its ability to format dates. You can use the -Format parameter to specify the date format you need. Here are some examples:

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

You can see the output in the screenshot below:

What is PowerShell Get-Date

This will return the date in the format MM/dd/yyyy, such as 09/15/2024.

Get-Date -Format "dddd, MMMM dd, yyyy"

This command will return the date in a more readable format like Sunday, September 15, 2024.

Check out Get Date Without Time in PowerShell

Date Arithmetic

You can perform date arithmetic using Get-Date. For instance, to get the date 5 days from now, you can use the AddDays method:

(Get-Date).AddDays(5)

Similarly, to get the date 3 months ago:

(Get-Date).AddMonths(-3)

Get Specific Parts of the Date

Sometimes, you might only need specific parts of the date, such as the year, month, or day. You can easily extract these using the properties of the DateTime object returned by Get-Date:

(Get-Date).Year
(Get-Date).Month
(Get-Date).Day

PowerShell Get-Date Examples

Now, let me show you a few examples of PowerShell Get-Date.

Calculate Age

Let’s say you want to calculate someone’s age based on their birthdate. Here’s how you can do it:

$birthdate = Get-Date "1990-01-01"
$today = Get-Date
$age = $today.Year - $birthdate.Year
if ($birthdate > $today.AddYears(-$age)) { $age-- }
$age

Schedule a Task

If you need to schedule a task to run at a specific time, you can calculate the time difference and use Start-Sleep:

$futureTime = (Get-Date).AddHours(2)
$timeDifference = $futureTime - (Get-Date)
Start-Sleep -Seconds $timeDifference.TotalSeconds
# Task to run after 2 hours
Write-Output "Task is running now!"

Convert Time Zones

You can also convert between time zones using Get-Date:

$utcTime = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
$localTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([System.DateTime]::Parse($utcTime), "Pacific Standard Time")
$localTime

Conclusion

In this tutorial, I have explained everything about the Get-Date PowerShell cmdlet and shown a few real examples of the PowerShell Get-Date.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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