2

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.

1 Answer 1

7

You are looking for the Switch parameter:

Switch parameters are parameters with no parameter value. Source.

Example:

Param(
    [Parameter(Mandatory=$false)]
    [Switch]
    $Foo
)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, but weired. I always thought, a Boolean expression behaves like a switch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.