In PowerShell it’s very common to use switch parameters. The peculiarity of these parameters is that when you use them, you specify the parameter name but don’t provide any value, since it’s assumed that its value will be $true when used and $false if not specified. Typical examples are -Recurse or -Force.

This would be an example with this type of parameter:

Function Do-Something([Switch]$Recurse) {
    Write-Host $Recurse
}

Do-Something -Recurse
Do-Something

If I run this script, I’ll see $True and $False on screen, as I might expect. If I make a few changes to the code, the result is still as expected:

Function Do-Something([Switch]$Recurse) {
    if ($Recurse.IsPresent) {
        Write-Host "I'll do it recursively"
    } Else {
        Write-Host "I won't do it recursively"
    }
}

Do-Something -Recurse
Do-Something

So far so good, but actually I haven’t been doing it the recommended way. If I happened to write a Pester test where I had to check $Recurse like this:

$Recurse | Should Be $True

I’d get an unexpected result, because $Recurse isn’t actually a Boolean. Calling GetType() shows that it’s actually of type System.Management.Automation.SwitchParameter. The right way to check whether the parameter was passed is this:

$Recurse.IsPresent | Should Be $True

And if you ever need to treat it as a boolean, you can always use the ToBool() method.