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

PowerShell
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
Get-Date -Format with sortable, log line, filename-safe, UK and ISO 8601 formats in PowerShell

The first four, run one after another:

Get-Date -Format run four times in PowerShell, returning 2026-11-16, 2026-11-16 12:59:24, 20261116-125924 and 16/11/2026
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.

PowerShell
$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

PowerShell
(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:

PowerShell
(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.

If you know what the date should look like but not the string for it, give Claude an example such as 24 Aug 2026 15:29 and ask for the format. Run it through Get-Date -Format once before it goes into a filename.

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 *