Converting VBScript's FormatDateTime Function

Definition: Returns an expression formatted as a date or time.


The .NET Framework enables you to create a vast array of custom date formats, far too many to cover in this introductory guide. Fortunately, you can create the four basic date-time formats without having to use any fancy (and sometimes confusing) custom formatting strings. Instead, just employ the four methods shown below:

$a = (get-date).tolongdatestring()
$a = (get-date).toshortdatestring()
$a = (get-date).tolongtimestring()
$a = (get-date).toshorttimestring()

When you run these commands and then echo back the values of $a you should get something similar to the following, in order:

Wednesday, September 13, 2006
7:57:25 PM
7:57 PM

Return to the VBScript to Windows PowerShell home page