PowerShell Genie

PowerShell Dates

Get-Date returns a real DateTime object, not just text. Format it with -Format or .ToString(), shift it with .AddDays(), compare two dates with -gt, and measure the gap with New-TimeSpan.

Lesson 3 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Overview

Get-Date answers two questions: what time is it now, and what does a date look like in the shape you need. With no arguments it returns the current date and time as a DateTime object, which means you can do maths on it instead of parsing text.

Formatting turns a DateTime into text. Get-Date -Format 'yyyy-MM-dd' gives 2026-10-28, and Get-Date -Format 'HH:mm' gives the 24-hour time. The letters are case-sensitive: MM is month, mm is minutes, HH is 24-hour, hh is 12-hour. For file names, yyyy-MM-dd_HH-mm-ss is safe because it contains no characters Windows forbids.

Date maths uses methods, not operators. .AddDays(7) moves a week forward, .AddHours(-2) moves two hours back. Comparing dates works directly: (Get-Date) -gt $deadline is true when now is past the deadline. New-TimeSpan measures the gap between two dates in days, hours and minutes.

Inspect this example’s structure

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Get-Date -Format 'yyyy-MM-dd' (Get-Date).AddDays(7)
  1. Stage 1Get-Date -Format 'yyyy-MM-dd'Prints today's date in year-month-day order.

    What happens

    The first line prints today's date in year-month-day order, the shape that sorts correctly as text. The second line takes the current date and time and returns a DateTime seven days in the future — the original value is untouched, because DateTime values are immutable and AddDays returns a new one.

    Worth remembering

    • Get-Date returns a DateTime object, not plain text.
    • -Format uses case-sensitive letters: MM is month, mm is minutes.
    • yyyy-MM-dd sorts correctly and is safe in file names.
    • .AddDays() and .AddHours() shift dates; they return a new value.
    • Compare dates directly with -gt, -lt and -eq.
    • New-TimeSpan measures the gap between two dates.

    Primary documentation

    Get-Date documentation

    Try it

    Illustrative output · values vary by environment

    1PS> Get-Date -Format 'yyyy-MM-dd'

    Example reader

    Inspect this page's example

    Fictional browser data when supported

    Run supported commands against the practice data, or inspect the example without executing it.

    Ready to inspect.

      Prints today's date in year-month-day order.

      Show reference output

      Illustrative output · values vary by environment

      2026-10-28

      Keep going

      Next: PowerShell Event Logs
      Genie guidePowerShell Dates

      Get-Date returns a DateTime object, not text. Format it with -Format, shift it with .AddDays(), and compare dates directly with -gt and -lt.