HOWTO · PowerShell
How to Get Date and Time Using PowerShell
This article will explain how we can get the current date and time, and how the DateTime object works in PowerShell.
On this page
PowerShell’s Get-Date cmdlet retrieves the current date and time as a System.DateTime value. You can inspect its properties, calculate a new date with methods such as AddDays(), compare date values, convert a string with [DateTime], and format a value with -Format.
Get Date and Time in PowerShell
Call Get-Date to retrieve the current date and time. Assigning the result to a variable makes it easy to inspect or reuse.
$now = Get-Date
$now
The displayed date and time reflect the machine’s clock, local time zone, and culture, so they vary between runs. Get-Date returns a System.DateTime object, not just a text string. Select properties such as Year, DayOfWeek, and DayOfYear to inspect parts of the value:
$now = Get-Date
$now | Select-Object Year, Month, Day, DayOfWeek, DayOfYear, Hour, Minute
To see the object’s type, use GetType(). To inspect its available properties and methods, pipe it to Get-Member:
$now = Get-Date
$now.GetType().FullName
$now | Get-Member
The type name is System.DateTime. Use dot notation, such as $now.Year, when you need an individual property in another expression.
Date Arithmetic Using DateTime Methods in PowerShell
A System.DateTime value provides methods for moving forward or backward from a starting date. Define the starting value explicitly when you want a repeatable calculation; AddDays(), AddHours(), and AddYears() return new date-time values and leave the original value unchanged.
$start = [datetime]::new(2024, 5, 10, 9, 0, 0)
$start.AddDays(8)
$start.AddHours(3)
$start.AddYears(5)
$start.AddDays(-7)
A negative argument subtracts the corresponding amount of time. These results are based on $start, so changing the starting value changes each result. DateTime also has methods such as AddMonths() and AddMinutes() for other intervals.
Compare Dates in PowerShell
PowerShell’s -lt and -gt operators test whether one date-time value is earlier or later than another. Use values with a clear source, such as Get-Date for the current moment or a parsed date string.
$first = [datetime]::new(2024, 5, 10)
$second = [datetime]::new(2024, 5, 11)
$first -lt $second
$first -gt $second
Each comparison returns a Boolean value: -lt is true when the left value is earlier, and -gt is true when it is later. The examples use dates at midnight because no time was supplied to the constructor.
Convert Strings to DateTime Object in PowerShell
Cast a string to [DateTime] when its contents represent a date and time:
$dateText = "2024-05-10T13:35:08"
$date = [DateTime]$dateText
$date.GetType().FullName
$date
The cast asks PowerShell and .NET to interpret the text as a date. Use an unambiguous input format and account for the current culture when accepting user-entered strings. A trailing Z identifies the input timestamp as UTC. When PowerShell casts that text to [DateTime], .NET may convert the represented instant to the machine’s local time; the resulting value can have Local kind and display a local clock time rather than the UTC fields in the input. The displayed value therefore depends on the machine’s time zone. If you need to retain the timestamp’s explicit offset, parse it as DateTimeOffset; a Z timestamp is represented with a +00:00 offset.
DateTime Methods in PowerShell
For common date and time strings, DateTime provides ToLongDateString(), ToShortDateString(), ToLongTimeString(), and ToShortTimeString(). Each method returns a string using the current culture’s conventions.
This example defines its own value, then combines the short date and short time forms:
$date = [datetime]::new(2024, 5, 10, 9, 30, 0)
$date.ToShortDateString() + " " + $date.ToShortTimeString()
Because these methods use the current culture, separators and ordering can differ across systems. Choose an explicit format string when a particular layout is required.
Format DateTime Objects in PowerShell
Use Get-Date -Format to turn a date-time value into a string with a chosen layout. Pass an existing value with -Date, or omit it to format the current date and time.
$date = [datetime]::new(2024, 5, 10, 21, 21, 0)
Get-Date -Date $date -Format "dddd dd-MM-yyyy HH:mm"
In this pattern, dddd is the full weekday name, dd is the day, MM is the month, yyyy is the four-digit year, and HH:mm is the 24-hour time. Names and date conventions depend on the current culture. For a value tied to an instant, time-zone conversion can also affect the displayed local time; include a suitable offset format such as K when that information is useful.