Colyn1337
Colyn1337

Reputation: 1723

How to Create a Named Only Parameter for Powershell?

I'm trying to create a powershell function and I'm including input parameters. Among other things, there's one Param that I only care if it's been named (it's value doesn't matter, only that the user used it). I'm having difficulty getting powershell to work that way as it keeps expecting the param to have an argument.

I've read the Parameter Attribute Declaration page from Microsoft, which doesn't appear to address named (only) parameters. This is my test function:

Function testme
{
    Param
    (
        $Test
    )

    If($Test -eq $null)
    {
        Write-Host "Test was null!"
    }
    else
    {
        Write-Host "Test was not null!"
    }
}

My question is simply, how do I create a named only parameter?

Upvotes: 0

Views: 76

Answers (1)

Patrick
Patrick

Reputation: 2208

Look here: http://technet.microsoft.com/de-de/magazine/jj554301.aspx
I think u mean a Switch? It works like this:

Function Test() {
    Param(
        [switch]$DoSomething
    )

      If ($DoSomething) {
        Write-Host -Object "Test"
      }
}

Test -DoSomething
Test

Upvotes: 1

Related Questions