Understanding and Manipulating Dates and Time in PowerShell

Working with Dates and Times in PowerShell is very common. Many scripts require simple or complex date calculations for such things as archiving files or logic to determine what data to operate on. Learn how to effectively work with dates and times in PowerShell in this article using the Get-Date cmdlet, DateTime class, and the New-TimeSpan cmdlet.

Using the Get-Date Command

The number one command to consider when working with dates and times in PowerShell is the Get-Date command. By running Get-Date just by itself, you will output a DateTime object with the current date and time. Using the Get-Member command, we can see that the output is a System.DateTime object type which has several built-in functions.

Outputting a DateTime object using the Get-Date cmdlet.

So how would we return a very specific date? We can simply pass in a date and time to have a proper DateTime object returned as seen below.

Get-Date -Date "10/29/2020 12:00:00"
Outputting a specific date from the Get-Date cmdlet.

Of course, we often want to perform a date calculation from the current time. How would we go about subtracting or adding time to the current date? With the DateTime object type, there are several built-in methods that make this very easy (non-exhaustive list below).

Sponsored Content

What is “Inside Microsoft Teams”?

“Inside Microsoft Teams” is a webcast series, now in Season 4 for IT pros hosted by Microsoft Product Manager, Stephen Rose. Stephen & his guests comprised of customers, partners, and real-world experts share best practices of planning, deploying, adopting, managing, and securing Teams. You can watch any episode at your convenience, find resources, blogs, reviews of accessories certified for Teams, bonus clips, and information regarding upcoming live broadcasts. Our next episode, “Polaris Inc., and Microsoft Teams- Reinventing how we work and play” will be airing on Oct. 28th from 10-11am PST.

  • AddDays
  • AddHours
  • AddSeconds
  • AddMonths
  • AddYears

To use this, let us show the example of adding two days and five hours to a date. We can chain the AddDays and the Addhours methods onto our date stored in the variable $CurrentDate.

$CurrentDate = Get-Date
Demonstrating adding time using the AddDays and AddHours methods for a DateTime object.

How would we subtract time from a date? All of these methods have “add” in the name and not subtract. To subtract time, we simply need to pass in negative values.

$CurrentDate = Get-Date
Demonstrating subtracting time using the AddDays and AddHours methods for the DateTime object.

Formatting Dates and Times

There are two ways to output a specific date-time format. You can use either the -Format parameter or the -UFormat parameter. The latter parameter uses the Unix formatting syntax whereas the former uses .NET formatting syntax. In the below code example we demonstrate an example using both. There are many different formatting identifiers available, and we will not list them all here but can be referenced here.

Get-Date -Format "yyyyMMdd_hhmmss"
Get-Date -UFormat "%Y%m%d_%H%M%S"
Demonstrate using the same format for both .NET and Unix formatting strings in Get-Date.

Using Get-Culture in Get-Date

Different operating system languages may use different date and time formats. When you use Get-Culture you are able to return a localization object that PowerShell can use to return a localized string. This means that you will have to convert the DateTime object to a string type and pass the localization that you want.

(Get-Date).ToString("dddd, yyyy-MM-dd",(Get-Culture -Name 'en-US'))
(Get-Date).ToString("dddd, yyyy-MM-dd",(Get-Culture -Name 'nl-NL'))
Demonstrating localization in dates.

As you can see, Thursday is now returned in the Dutch language using the nl-NL encoding.

The DateTime Object Class

Since we know that the Get-Date command outputs a DateTime object, is there more that we can do with this object? Not that we will go into every method, but there are a few methods that are very useful to use. The following are a handful of functions that are available for use with an existing DateTime objects.

  • IsDaylightSavingTime
  • ToFileTime and ToFileTimeUTC
  • ToUniversalTime
Demonstrating additional useful DateTime object methods.

There are a handful of static functions that are very useful as well. These can be used without a declared DateTime object.

  • DaysInMonth
  • Parse and ParseExact

The first method simply returns the number of days in any given month when passed a year and month number. Parse attempts to create a DateTime object out of a given string using common formats. What if the string you are passing in doesn’t fit any known format? You can use ParseExact to do so by giving a date and time template to follow.

[DateTime]::DaysInMonth(2020, 10)
[DateTime]::ParseExact("10 10 2020","MM dd yyyy", $Null)
Demonstrating DateTime static functions.

The date 10 10 2020 is not one that the .NET parser knows about as spaces are not typically used as separators. Therefore, we pass in the template MM dd yyyy which informs the parser how to look at the string and properly return a DateTime object.


Finally, we are going to quickly touch on TimeSpan objects. This is not technically a date and time, but rather a span of time between two given points. You can pass in a number of different times in to generate a timespan object that includes the given interval using the New-TimeSpan cmdlet. You can also combine this with Get-Date to specify a start and end date (if no start is given, the current date and time is used), then you can determine the number of days until that point.

New-TimeSpan -Days 30
New-TimeSpan -End (Get-Date "12/31/2020")
Demonstrating DateTime static functions.


There are many ways to work with dates and times in PowerShell and there are even more methods and techniques that can take your script writing to the next level. Leverage the extensive power of Get-Date, New-TimeSpan, and the DateTime object to create advanced scripts that handle dates and times with ease.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

Entrepreneur, hustler, husband, dad, Automator, content producer, published author, Microsoft MVP, DevOps pro and passionate problem-solver.
External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: