Gill-Bates
Gill-Bates

Reputation: 679

Powershell: How to pass Boolean Value into Function Parameter?

I've created a function in Powershell and want to pass the Parameter -Foo as a Boolean.

I've simplified my Use-Case for Visualization:

function Get-Foobar {

    [CmdletBinding()]
    param (
        [bool]$Foo
    )

    if ($Foo) {
        Write-Host "Foo!"
    }
    else {
        Write-Host "Bar!"
    }
}

The function works as expected, when I call the function Get-Foobar -Foo $true. But this is not what I want. Just Get-Foobar -Foo should suffice.

Referring to the documentation, specifying the parameter when calling the function should already return a true. But apparently this does not work.

Upvotes: 2

Views: 2244

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You are looking for the Switch parameter:

Switch parameters are parameters with no parameter value. Source.

Example:

Param(
    [Parameter(Mandatory=$false)]
    [Switch]
    $Foo
)

Upvotes: 7

Related Questions