PowerShell, call script with parameter without a value, like a script with options

powershell

Solution

Set the type of your parameter to `[switch]`, e.g.

param (
    [switch]$lastmonth
);

EDIT: Note that the variable will be a boolean. You can test it like:

if ($lastMonth) {
    Write-Host "lastMonth is set."
}
else {
    Write-Host "lastMonth is not set."
}

(thanks Chris)

Problem

Is it possible to call a PowerShell script with options? Like a parameter without a value. For instance I currently use: ``` param ( $lastmonth=0 ); ``` in the script. Now I can use the call ``` script.ps1 -lastmonth 1 ``` or ``` script.ps1 ``` and use $lastmonth for controlling the flow. But I'd like to have the calls like ``` script.ps1 -lastmonth ``` and ``` script.ps1 ``` and control the flow depending on whether -lastmonth was given, or not.

Original source