Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell Dates, Times and Formats

Posted on October 8, 2014October 8, 2014

astroclock_thumbIf you are like me you use date time values constantly in PowerShell. From simply displaying the current date and time in progress message to using different values to create file or folder names. The Get-Date cmdlet has a -Format parameter which you can use. The tricky part is remembering what values to specify. Especially because they are case-sensitive. The values are documented at http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo%28VS.85%29.aspx.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

So to make my life (and maybe yours) easier, I wrote a little PowerShell script to remind me of the possible values.

$patterns = "d","D","g","G","f","F","m","o","r","s", "t","T","u","U","Y","dd","MM","yyyy","yy","hh","mm","ss","yyyyMMdd","yyyyMMddhhmm","yyyyMMddhhmmss"

Write-host "It is now $(Get-Date)" -ForegroundColor Green

foreach ($pattern in $patterns) {
#display text
"{0}`t{1}" -f $pattern,(Get-Date -Format $pattern)

} #foreach

Write-Host "Most patterns are case sensitive" -ForegroundColor Green

The variable $patterns is an array of commonly used datetime format values. Many of them were pulled from that MSDN page but I added some of my own at the end. Here's the output from this script.
test-datetimepattern01

This looks nice, and is definitely a nice cheat sheet. But then I realized since I'm writing a script I should take this to the next level and write a useful object to the pipeline.

$patterns = "d","D","g","G","f","F","m","o","r","s", "t","T","u","U","Y","dd","MM","yyyy","yy","hh","mm","ss","yyyyMMdd","yyyyMMddhhmm","yyyyMMddhhmmss"

Write-host "It is now $(Get-Date)" -ForegroundColor Green

foreach ($pattern in $patterns) {

#create an Object
[pscustomobject]@{
 Pattern = $pattern
 Syntax = "Get-Date -format '$pattern'"
 Value = (Get-Date -Format $pattern)
}

} #foreach

Write-Host "Most patterns are case sensitive" -ForegroundColor Green

This version creates a custom object for each pattern, including the syntax.
test-datetimepattern02

An alternative to -Format is to use the ToStringMethod() specifying the format pattern.

PS C:\> (get-date).ToString('o')
2014-10-08T07:58:46.0376740-04:00

Because this is an object I can pipe it to other cmdlets.

C:\scripts\Test-DatePattern.ps1 | format-table

test-datetimepattern03

I included the syntax so all you need to do is copy and paste. Or, how about this?

Function Insert-Pattern {
C:\scripts\Test-DatePattern.ps1 | Out-GridView -Title "Select a format" -OutputMode Single |
foreach {
  $psise.CurrentFile.Editor.InsertText($_.Syntax)
}
}

Define this function in the PowerShell ISE, perhaps even adding it to your Add-Ons menu with a keyboard shortcut. When executed, you'll get a graphical display using Out-Gridview.

test-datetimepattern04

Select a pattern and the syntax gets pasted into your PowerShell script. I love making PowerShell do my work for me! Enjoy.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d