Converting VBScript's Month Function

Definition: Returns a whole number between 1 and 12, inclusive, representing the month of the year.

Month

Need to get the number representing the month for a specified date (e.g., 1 for January, 2 for February, etc.)? Then just use the Get-Date Cmdlet, the -f parameter and the MM .NET formatting command. This command returns a numeric value representing the month of the year for the current date:

$a = get-date -f "MM"

When you run this command (at least during the month of September) and then echo back the value of $a you should get the following:

09

If you don’t want the leading zero, then retrieve the month value and change it to an integer data type. How do you do that? Like this:

$a = [int] (get-date -f "MM")

Return to the VBScript to Windows PowerShell home page