PowerShell One-Liner: Quick Customization of the Date and Time Display
Using the quick formatting options of the Get-Date cmdlet turns a ho-hum Date and Time display into something extra powerful and potentially exciting.
July 18, 2013
In the first article in this series on Date and Time entitled PowerShell One-Liner: Displaying the Date, I went over the basics for displaying the Time and Date in your PowerShell scripts using the simple Get-Date cmdlet. But, PowerShell wouldn't be PowerShell if it didn't give you additional options. The Get-Date cmdlet comes with a stack of format indicators that can be used to change how the Time and Date displays.
As I stated in the first article:
But, what if you want the date displayed in a different way? Obviously, the basic formatting of the Get-Date cmdlet alone would be semi-difficult to parse as a variable for the rest of a script. For example, say you are writing a script that retrieves all files in a specific folder, makes a modification to each one, and then resets the date flag. Parsing the basic output for the Get-Date cmdlet would take a bit longer than if the date actually read: 7.16.2013.
Your options for formatting the Time and Date output are as follows…
d – Short date format (example: 7/18/2013)
D – Long date format (example: Thursday, July 18, 2013)
f – Long date followed by short time (example: Thursday, July 18, 2013 11:02 AM)
F – Long date followed by long time (example: Thursday, July 18, 2013 11:02:26 AM)
g – Short date followed by short time (example: 7/18/2013 11:02 AM)
G – Short date followed by long time (example: 7/18/2013 11:02:26 AM)
M or m – The month and the day (example: July 18)
s – The ISO 8601 formatted date and time (example: 2013-07-18T11:02:26)
t – Short time format (example: 11:02 AM)
T – Long time (including seconds) (example: 11:02:26 AM)
u – Universal date and time display (example: 2013-07-18 11:02:26Z)
U – Universal time using the long time and long date format (example: Thursday, July 18, 2013 11:02:26 AM)
Y or y – The month and year (example: July, 2013)
Right away, you'll notice that, for a number of the format options (except for Y and M), capital and lowercase letters produce different formatting displays. For example, if you apply both the f and the F, the output varies as shown below…
The next step to understand, I gave away in the screenshot above. To utilize any of the Time and Date formatting switches, you simply use -format (dash and the word, format) with the Get-Date cmdlet and then supply the display option at the end. So, the finished PowerShell one-liner would look something like this:
Get-Date -format y
And, the output would look like this:
And, just like many of the output type functions in PowerShell, you can assign your favorite formatted Date and Time to a variable so you only have to call the variable later on in your scripts. This is an extremely useful thing to do should you need to have access to multiple Date and Time formats throughout your finished script. For example, you may want to display the Date and Time in one way to end-users in a dialog box, but use another, easier to parse format for the inner workings of the PowerShell script. That's just one example.
Using the quick formatting options of the Get-Date cmdlet turns a ho-hum Date and Time display into something extra powerful and potentially exciting.
About the Author
You May Also Like