🎓 A1Learner
🔍
0 of 52 lessons completed

PowerShell Dates and Times

Dates and times are values that name a day and a clock time. Get-Date is the cmdlet that works with dates, and it takes a fixed date with -Date.

Print a fixed date

Give Get-Date a fixed date and a format. The output is the same on every run.

Get-Date -Date "2026-10-28" -Format "yyyy-MM-dd"

Shift a date forward

AddDays returns a date seven days later. ToString with a format prints it in the same shape.

(Get-Date -Date "2026-10-28").AddDays(7).ToString("yyyy-MM-dd")

Read a date's parts

DayOfWeek and Year are properties of a date value. Reading them prints one value each.

(Get-Date -Date "2026-10-28").DayOfWeek
(Get-Date -Date "2026-10-28").Year

Example

$start = Get-Date -Date "2026-10-28"
$deadline = $start.AddDays(14)
"Start: " + $start.ToString("yyyy-MM-dd")
"Deadline: " + $deadline.ToString("yyyy-MM-dd")
"Deadline day: " + $deadline.DayOfWeek

Key points

  • Get-Date: the cmdlet that creates or reads a date value.
  • Format: the -Format parameter prints a date in a chosen shape, such as yyyy-MM-dd.
  • AddDays: a method that returns a new date shifted by a number of days.
  • DayOfWeek: a property that names the weekday of a date, such as Wednesday.
  • Year: a property that gives the year number of a date.

In this example, a start date is stored in a variable. AddDays(14) makes a deadline two weeks later, and the last three lines print the start date, the deadline, and the weekday of the deadline.

Note

use a fixed -Date literal in examples, never the current date, so the output never changes.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does (Get-Date -Date "2026-10-28").AddDays(7).ToString("yyyy-MM-dd") print?