The Complete Guide to Getting the Current Date in PowerShell
As a full-stack developer and professional coder, working with dates and times is a common task I face regularly in PowerShell. Whether it‘s displaying the current date on a dashboard, scheduling scripts, determining ages, or countless other use cases, PowerShell makes accessing and manipulating dates and times easy with the flexible Get-Date
cmdlet.
In this comprehensive 3,000+ word guide, you‘ll learn the ins and outs of using Get-Date
to handle the current date and time in PowerShell. I‘ll cover:
- Displaying the current date and time
- Custom date and time formats
- Date math and manipulations
- Daylight Saving Time handling
- Saving output to variables
- Use cases and cool tricks
- Dotnet, UNIX and legacy alternatives
- And more!
By the end, you‘ll master all aspects of using Get-Date
in PowerShell. Let‘s dive in!
Displaying the Current Date and Time
The most basic usage of Get-Date
is to display the current date and time in PowerShell.
Simply call Get-Date
with no parameters:
Get-Date
This returns a DateTime
object displaying the current date and time according to your PowerShell environment‘s culture settings:
Tuesday, March 7, 2023 9:15:32 PM
Fun Fact: PowerShell sets the default culture based on the OS locale. So Spanish Windows will return Spanish date names.
You can override this by appending a .ToString()
and passing a specific culture.
For example, forcing US English:
Get-Date.ToString([System.Globalization.CultureInfo]::InvariantCulture)
On multi-language teams, be aware Get-Date
‘s output may vary by developer!
Now let‘s look at accessing just the date and time pieces.
Returning Just Date or Time
When calling Get-Date
, the default return includes both the date and time concatenate together.
You can instead return just the date or time using the -DisplayHint
parameter:
Date Only
Get-Date -DisplayHint Date
Returns:
Tuesday, March 7, 2023
Time Only
Get-Date -DisplayHint Time
Returns:
10:15:32 PM
Date and Time Separate
Get-Date -DisplayHint DateTime
Returns:
Tuesday, March 7, 2023 10:15:32 PM
This separates the date and time, perfect for UI displays.
-DisplayHint lets you filter down to just the date and time components needed.
Custom Date and Time Formatting
While the defaults work, Get-Date
also supports custom date and time formatting for ultimate flexibility.
The supported formats include:
- .NET Formatting – dddd, MM, dd, etc.
- UNIX Formatting – YYYY-MM-DD HH:MM
- UFormat Specifiers – %A, %m, %d, etc.
Let‘s explore these popular ways to customize formats.
.NET Formatting
The most common approach is .NET date and time format strings.
Here are some useful .NET formats:
.NET Format | Meaning | Example |
---|---|---|
dddd | Full weekday name | Tuesday |
MM | 2-digit month number | 03 for March |
dd | 2-digit day number | 07 |
yy | 2-digit year | 23 for 2023 |
yyyy | 4-digit year | 2023 |
HH | 2-digit 0-padded hour | 05, 14 |
mm | 2-digit 0-padded minute | 07, 26 |
ss | 2-digit 0-padded second | 09, 36 |
tt | AM/PM marker | AM, PM |
Pass any .NET formats to -Format
to customize the output:
Get-Date -Format "dddd MM/dd/yyyy HH:mm"
Returns:
Tuesday 03/07/2023 22:26
The flexibility here is limitless. You have full control over the exact date/time format.
UNIX Formatting
If you want compatibility with UNIX/Linux tools that expect ISO 8601 standard datetime strings, use -Format u
:
Get-Date -Format u
Returns:
2023-03-07T22:26:09Z
This ensures maximum open-source ecosystem compatibility.
UFormat Specifiers
Lastly, PowerShell offers its own UFormat
s tailored to date and time output:
Get-Date -UFormat "%A %m/%d/%Y %R %Z"
Common specifiers:
Specifier | Meaning | Example |
---|---|---|
%A | Full weekday name | Monday |
%a | Abbreviated weekday name | Mon |
%m | 2-digit month number | 03 for March |
%d | 2-digit day number | 07 |
%Y | 4-digit year | 2023 |
%y | 2-digit year | 23 for 2023 |
%H | 2-digit 0-padded hour | 05, 14 |
%M | 2-digit 0-padded minute | 07, 26 |
%S | 2-digit 0-padded second | 09, 36 |
%Z | Offset from UTC | -0800 |
Again, full customization of the exact output string format needed.
Between .NET, UNIX and UFormats – you can craft any date/time format required.
Manipulating Dates and Times
Beyond mere display, you can also add, remove or compare dates with Get-Date
.
A few examples of manipulating dates:
Date Math
Add or subtract days:
(Get-Date).AddDays(7)
(Get-Date).AddDays(-3)
Add or subtract hours:
(Get-Date).AddHours(2)
(Get-Date).AddHours(-1)
Truncate times by rounding:
Get-Date -Hour 0 -Minute 0 -Second 0
Tons of convenient math functions are available!
Comparisons
Compare dates with logic operators:
$today = Get-Date
$someDay = [DateTime]"2025-12-25"
if ($someDay -gt $today) {
"Christmas is in the future"
} else {
"Christmas is in the past"
}
You can compare dates, times and datetimes easily.
Time Components
Extract components:
$now = Get-Date
$now.DayOfWeek # Wednesday
$now.Day # 5
$now.Hour # 14
$now.Minute # 23
Need just the hour? Just the weekday name? Easily extract any component.
As you can see, common date and time manipulations are trivial with a few PowerShell commands!
Daylight Saving Time Handling
An important aspect when working with dates is proper Daylight Saving Time (DST) handling.
Does a date have DST enabled?
Use .IsDaylightSavingTime()
:
$dt = Get-Date
$dt.IsDaylightSavingTime() # $True if DST is in effect, $False otherwise
You can also direct compare times with and without DST:
# Date with DST
$dstDate = [DateTime]"2023-06-21"
# Date without DST
$nonDstDate = [DateTime]"2023-01-15"
# Compare difference
($dstDate - $nonDstDate).TotalHours # Returns 23 hours
Now your scripts can programmatically handle DST!
Saving Output to Variables
When working with dates and times, you‘ll often want to save the output for later reuse.
For example, capture current date:
$today = Get-Date
Then reuse it:
$today.DayOfWeek # Wednesday
$today.AddDays(1) # Tomorrow‘s date
$today.ToString(‘d MMM yy‘)# 05 Mar 23
Saving Get-Date
to a variable avoids costly re-calls and provides a handle to that moment in time.
Some great examples:
- Start of script datetime for logging
- Basis for time-based script logic (runs Mon-Fri)
- Birthdays passed to functions
- Age calculations from saved datetimes
The use cases are endless!
Cool Tricks and Use Cases
Let‘s round things out by exploring some cool tricks and use cases for using Get-Date
:
Log Message Timestamps
Auto-include readable timestamps in logs:
$now = Get-Date -Format "MM-dd HH:mm:ss"
Write-Host "[$now] Starting script RunBackup.ps1"
Gives you:
[03-07 22:30:05] Starting script RunBackup.ps1
Dynamic Greeting Messages
Display personalized date-based greetings:
$hour = (Get-Date).Hour
if ($hour -le 12) {
"Good morning!"
} elseif ($hour -le 17) {
"Good afternoon!"
} else {
"Good evening!"
}
Age Calculations
Calculate ages from birthdates:
$birthDate = [DateTime]"1980-02-11"
$age = (Get-Date) - $birthDate | Select -Expand TotalDays | ForEach-Object { [int]($_/365) }
"John is $age years old"
Handy for HR systems!
As you can imagine, countless innovative applications exist working with dates and times in PowerShell scripts!
Dotnet and Other Alternatives
I want to briefly cover some alternatives to Get-Date
in PowerShell for completeness:
.NET DateTime Constructors
PowerShell being built on .NET means all the [System.DateTime] constructors are available:
[DateTime]::Now # Current datetime
[DateTime]::Today # Current date
[DateTime]::UtcNow # UTC datetimes
[System.DaylightTime] Type Accelerator
Gets the current date including handling DST properly:
[System.DaylightTime]::CurrentDate
Legacy [DateTime] Class
Lastly, older PowerShell code may use the legacy [DateTime]
class:
[DateTime]::Now
This still works but lacks some newer methods added to Get-Date
.
In most cases, as a full-stack developer, Get-Date
is my tool of choice for its balance of usability and flexibility working with dates and times in PowerShell.
Conclusion
We‘ve covered a ton of ground around exploiting PowerShell‘s Get-Date
cmdlet – from display formats, math, comparisons, variables and more!
Here are some key takeaways:
- Access the current datetime with
Get-Date
- Customize display formats using
.NET
,UNIX
andUFormat
specifiers - Easily do date comparisons, math and DST checks
- Save
Get-Date
output to handy variables for reuse - Timestamp logs, calculate ages and greet users dynamically based on dates/times!
I‘m sure you now have lots of ideas for incorporating Get-Date
into your scripts!
Let me know if you have any other questions. And happy time scripting!