Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:24:09+00:00 2026-06-04T15:24:09+00:00

Is there a way to have some of the parameters mandatory based on some

  • 0

Is there a way to have some of the parameters mandatory based on some condition (for example, if one of the parameters is absent or false) in a PowerShell function?

My idea is to be able to call a function in two ways. A concrete example is a function that gets a list from SharePoint – I should be able to call it with relative URL of the list (one and only parameter) OR with a web URL and a list display name (two parameters, both mandatory, but only if list relative URL is not used).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:24:11+00:00Added an answer on June 4, 2026 at 3:24 pm

    As Christian indicated, this can be accomplished via ParameterSetNames. Take a look at this example:

    function Get-MySPWeb {
        [CmdletBinding(DefaultParameterSetName="set1")]
        param (
            [parameter(ParameterSetName="set1")] $RelativeUrl,
            [parameter(ParameterSetName="set2")] $WebUrl,
            [parameter(ParameterSetName="set2", Mandatory=$true)] $DisplayName
        )
        Write-Host ("Parameter set in action: " + $PSCmdlet.ParameterSetName)
        Write-Host ("RelativeUrl: " + $RelativeUrl)
        Write-Host ("WebUrl: " + $WebUrl)
        Write-Host ("DisplayName: " + $DisplayName)
    }
    

    If you run it with -RelativeUrl Foo it will bind to “set1”. If you call this function without any parameters it will also bind to “set1”.

    (Note – when no parameters are provided in PowerShell v3 (with Windows 8 consumer preview) it will bind to “set1”, however it will error binding in PowerShell v2 unless you add [CmdletBinding(DefaultParameterSetName="set1")] to the parameter block. Thanks @x0n for the DefaultParameterSetName tip!)

    If you try to run it with a parameter value from both sets you will get an error.

    If you run it with -WebUrl Bar it will prompt you for a parameter value for DisplayName, because it’s a mandatory parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If we have some branch, B1, is there a way to create branch B2
I have some VirtualBox VMs running. Is there any way to programatically get the
I have some code to read from a pdf file. Is there a way
Is there a way to delay css from firing using jquery? I have some
Is there some way when sending this message to specify that I rather have
I have a question about SqlDataReader: Is there some way to modificated the values
Is there some way to replicate rails' link-to-unless-current? Ie. if i have a list
If I have an NSArray with some values in them. Is there a way
I have a data() object containing some json. Is there a way I can
I have a asp.net mvc application. Some pages require SSL, is there a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.