Outputting Nicely Formatted Dates

by Dec 22, 2008

Get-Date provides you with the current date and time. With the -format parameter, you can add style to it. For example, use -format with a lowercase d to just output a short date:

Get-Date -Format d

You can get a list of format characters directly at MSDN: http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(VS.80).aspx

As a result, you can create a custom date according to your format template:

Get-Date -format 'yyyy-MM-dd hh-mm-ss'

In just one line of code, you get time-stamped filenames for temporary or log files:

(Get-Date -format 'yyyy-MM-dd hh-mm-ss') + '.log'