How to Format Dates with Get-Date in PowerShell

📜Part of the PowerShell Complete Guide, every PowerShell post on this site, grouped by the job you are doing.
The short answer

Get-Date -Format 'yyyy-MM-dd'. Case matters: MM is months, mm is minutes.

Timestamped filenames, log lines, and anything that has to sort correctly all come back to the same handful of format strings.

The Ones Worth Memorising

Get-Date -Format 'yyyy-MM-dd'            # 2026-08-24        sortable date
Get-Date -Format 'yyyy-MM-dd HH:mm:ss'   # 2026-08-24 15:29:02   log line
Get-Date -Format 'yyyyMMdd-HHmmss'       # 20260824-152902       filename safe
Get-Date -Format 'dd/MM/yyyy'            # 24/08/2026        for humans, UK
Get-Date -Format 'HH:mm'                 # 15:29             24 hour clock

Real output from those, run together:

sortable : 2026-08-24 15:29:02
filename : 20260824-152902
ISO      : 2026-08-24T15:29:02
Case is the thing that catches everyone. MM is the month and mm is the minute. HH is a 24-hour clock, hh is 12-hour. A filename pattern of yyyy-mm-dd silently gives you the year, the minutes, and the day, and it looks almost right until you check.

Timestamped Filenames

The reason yyyyMMdd is worth learning: filenames in that shape sort chronologically in Explorer and in any script that sorts by name.

$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$log   = "D:\Logs\backup-$stamp.log"

Start-Transcript -Path $log
# ... work ...
Stop-Transcript
Never put a colon in a filename. HH:mm:ss is fine inside a log line and illegal in a Windows filename, which is why the filename pattern above uses HHmmss. This is one of the most common causes of “the script works until it tries to create the file”.

Doing Maths With Dates

(Get-Date).AddDays(-30)      # thirty days ago
(Get-Date).AddHours(-1)      # an hour ago
(Get-Date).AddMonths(1)      # next month

# how long since something happened
$age = (Get-Date) - (Get-Item "D:\Logs\app.log").LastWriteTime
"{0:N1} hours old" -f $age.TotalHours

ISO 8601, and Why It Matters

When a date is going into JSON, an API, or a database, use the round-trip format rather than a hand-built string:

(Get-Date).ToString('o')                     # 2026-08-24T15:29:02.1234567+01:00
(Get-Date).ToString('yyyy-MM-ddTHH:mm:ssZ')  # if you have already converted to UTC
(Get-Date).ToUniversalTime()                 # convert first, then format

The reason to bother: Get-Date with no format returns a string built from the machine’s culture settings, so the same script produces 24/08/2026 on one server and 8/24/2026 on another. Any format string, or the round-trip o, removes that problem entirely.

Related


If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for SQL Server?

This site started as a SQL Server blog in 2017 and the archive is still here. The new SQL Server writing, and all the scripts, moved to a site of their own.

sqldba.blogScripts, error library, wait types, SSMS
Browse by topic