Outputting Nicely Formatted Dates

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'

Posted Dec 22 2008, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.