# parameter: how to require either one or the other but not both

**URL:** <https://forums.powershell.org/t/parameter-how-to-require-either-one-or-the-other-but-not-both/1893>\
**Category:** PowerShell Help\
**Created:** [September 18, 2013, 3:18am UTC](https://forums.powershell.org/t/parameter-how-to-require-either-one-or-the-other-but-not-both/1893 "2013-09-18T03:18:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-selph](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@john-selph](https://forums.powershell.org/u/john-selph)\
**Post date:** [September 18, 2013, 3:18am UTC](https://forums.powershell.org/t/parameter-how-to-require-either-one-or-the-other-but-not-both/1893/1 "2013-09-18T03:18:04Z")

</div>

So I’m making my first cmdlet and I’m using the chance to rewrite an old vbscript and improve it. I want to require either a username, or an employeeid parameter. I can’t find the syntax for making param do this for me. Will I have to hand code it? Or is there a way to get it to do an either-or mandatory parameters?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 18, 2013, 3:31am UTC](https://forums.powershell.org/t/parameter-how-to-require-either-one-or-the-other-but-not-both/1893/2 "2013-09-18T03:31:34Z")

</div>

You can do this using parameter sets. By putting each parameter in one set and not the other, PowerShell will take care of the rest. You may want to define a default parameter set, as well:

```
function YourCmdletName
{
    [CmdletBinding(DefaultParameterSetName='ByUserName')]
    param (
        [Parameter(Mandatory = $true, ParameterSetName = 'ByUserName')]
        [System.String]
        $UserName,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByEmployeeId')]
        [System.String]
        $EmployeeId
    )

    # In the function's code, you can either test for whether $UserName and/or $EmployeedId are null, or
    # check $PSBoundParameters.ContainsKey('UserName'), or check $PSCmdlet.ParameterSetName to see whether it
    # is set to 'ByUserName' or 'ByEmployeeId'.
}
```

---

<div class="post-metadata">

**Author:** ![john-selph](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@john-selph](https://forums.powershell.org/u/john-selph)\
**Post date:** [September 18, 2013, 3:55am UTC](https://forums.powershell.org/t/parameter-how-to-require-either-one-or-the-other-but-not-both/1893/3 "2013-09-18T03:55:34Z")

</div>

Awesome, thanks for the quick answer. After watching both the MVA’s on powershell, I’m really enjoying how much you can do with it. It’s much better than vbscript.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://forums.powershell.org/t/parameter-how-to-require-either-one-or-the-other-but-not-both/1893/4 "2024-05-16T20:57:36Z")

</div>


