Converting VBScript's DateSerial Function

Windows PowerShell

Definition: Returns a Variant of subtype Date for a specified year, month, and day.


OK, we’re not sure how often (if ever) this one comes up, but the DateSerial function in VBScript enables you to create a date just by passing the appropriate values for the year, month, and day:

MyDate1 = DateSerial(2006, 12, 31)

(Hey, we said we didn’t know how often this comes up.)

You can achieve roughly the same effect in Windows PowerShell by calling the Get-Date Cmdlet and passing the appropriate values:

$a = get-date -y 2006 -mo 12 -day 31

Notice that, in Windows PowerShell, we need to include parameters for the year (-y), the month (-mo), and the day (-day). We can, however, abbreviate the year and the month to make the finished product more closely mimic the DateSerial function.

When you run this command and then echo back the value of $a you should get something similar to the following:

Sunday, December 31, 2006 1:27:42 PM

Return to the VBScript to Windows PowerShell home page