Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > Wrangling Dates and Times with PowerShell
July 08
Wrangling Dates and Times with PowerShell

PowerShell does a pretty good job handling dates and times due to its good foundation with .NET and its focus on being cool to use PowerShell users. For basic DateTime formatting the help for Get-Date shows some great, easy to use examples.

help Get-Date –Examples


There’s –Format, –UFormat, and –DisplayHint, and those are all before we get to the flexibility that is .tostring(). There’s a lot of options, sometimes it’s too many options. Sometimes trying to string together exact combination of time and date information I’m looking for is a lot of work. Once again, PowerShell is there for me in the form of .GetDateTimeFormats()


It lists out a collection of precanned DateTime formats you can select from:


You can use one of those formats just by selecting its index number:


That’s the good news. The bad news is that you have to specify which number you want, and there are 133 formats in the list. If the one that really tickles your fancy is #87 you have to count the lines until you get to it.

Until now.

I had danced this dance a few times, squinting to find that just perfect DateTime format I was looking for and counting the lines leading up to it. Then I put my PowerShell mojo to good use and came up with this little gem:

(Get-Date).GetDateTimeFormats() | foreach {$I = 0} { Write-Host "$I - $_" ; $I++ }

Edit 7/9/2019

A helpful reader suggested this even shinier gem:

(Get-Date).GetDateTimeFormats() | foreach-object -begin {$I = 0} -process {[pscustomobject]@{Index = $I;Value = $_}; $I++ }

This lists each format, along with its index number.


Once you’ve found the format of your dreams you can look to its left and see what its index is.



If you want to use it with a variable instead of Get-Date, it looks like this:


Now you have no excuses for getting exactly the DateTime format you want.




Dates with File output

Just to add my favorites... I tend to use get-date when setting up a regular file output (e.g. export-csv -NoTypeInformation -path "$home\documents\filename$(get-date -format yyyyMMdd).csv")

Then I found get-date -format filedate (or filedatetime)

While testing or outputting multiple files, I use filedatetime, otherwise I stick with filedate. I think filedate and filedatetime are PS 5.1 and up.

Anyhow... just an add to some great info :)
 on 7/9/2019 10:17 AM

Re: Dates with File output

Those are good tips. Thanks.

Todd O. KlindtNo presence information on 7/9/2019 1:53 PM

Wrangling Dates and Times with PowerShell

"‘Wrangling Dates and Times with PowerShell’ is a gem for anyone navigating time-related data. <a href="">ai write for us</a> The article offers clear, practical guidance on handling without date-time complexities.
 on 10/5/2023 12:34 AM

Re: Wrangling Dates and Times with PowerShell

Understanding the differences between these two is crucial for grasping the modern world's functioning. In this article, we will delve into the world of digital technology, exploring how it differs from analog technology and its impact on various aspects of our lives.
 on 10/23/2023 1:27 AM


Gaining an understanding of how these two differ is essential to understanding how the modern world operates.
 on 3/19/2024 9:27 PM


Your post sparked some fascinating ideas for me.
 on 4/13/2024 7:53 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.


Body *

Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person


Want a message when I reply to your comment? Put your Twitter handle here.