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 8877019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:21:53+00:00 2026-06-14T19:21:53+00:00

I have a PowerShell function which changes the registry key values. Code: param( [Parameter()]

  • 0

I have a PowerShell function which changes the registry key values. Code:

param(
    [Parameter()] [switch]$CreateNewChild,
    [Parameter(Mandatory=$true)] [string]$PropertyType
)

It has a parameter, “CreateNewChild”, and if that flag is set, the function will create the key property, even if it wasn’t found. The parameter “PropertyType” must be mandatory, but only if “CreateNewChild” flag has been set.

The question is, how do I make a parameter mandatory, but only if another parameter has been specified?

OK, I’ve been playing around with it. And this does work:

param(
  [Parameter(ParameterSetName="one")]
  [switch]$DoNotCreateNewChild,

  [string]$KeyPath,

  [string]$Name,

  [string]$NewValue,

  [Parameter(ParameterSetName="two")]
  [switch]$CreateNewChild,

  [Parameter(ParameterSetName="two",Mandatory=$true)]
  [string]$PropertyType
)

However, this means that $KeyPath, $Name and $NewValue are not mandatory any more. Setting “one” parameter set to mandatory breaks the code (“parameter set cannot be resolved” error). These parameter sets are confusing. I’m sure there is a way, but I can’t figure out how to do it.

  • 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-14T19:21:54+00:00Added an answer on June 14, 2026 at 7:21 pm

    You could group those parameters by defining a parameter set to accomplish this.

    param (
        [Parameter(ParameterSetName='One')][switch]$CreateNewChild,
        [Parameter(ParameterSetName='One',Mandatory=$true)][string]$PropertyType
    )
    

    Reference:

    https://devblogs.microsoft.com/powershell/powershell-v2-parametersets

    http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/30/use-parameter-sets-to-simplify-powershell-commands.aspx

    — Update —

    Here’s a snippet that mimics the functionality you’re looking for. The "Extra" parameter set will not be processed unless the -Favorite switch is called.

    [CmdletBinding(DefaultParametersetName='None')] 
    param( 
        [Parameter(Position=0,Mandatory=$true)] [string]$Age, 
        [Parameter(Position=1,Mandatory=$true)] [string]$Sex, 
        [Parameter(Position=2,Mandatory=$true)] [string]$Location,
        [Parameter(ParameterSetName='Extra',Mandatory=$false)][switch]$Favorite,      
        [Parameter(ParameterSetName='Extra',Mandatory=$true)][string]$FavoriteCar
    )
    
    $ParamSetName = $PsCmdLet.ParameterSetName
        
    Write-Output "Age: $age"
    Write-Output "Sex: $sex"
    Write-Output "Location: $Location"
    Write-Output "Favorite: $Favorite"
    Write-Output "Favorite Car: $FavoriteCar"
    Write-Output "ParamSetName: $ParamSetName"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PowerShell script: param ( [Parameter(Mandatory=$true)][string]$input, [Parameter(Mandatory=$true)][string]$table ) Write-Host Args: $Args.Length Get-Content
Say I have MyScript.ps1 : [cmdletbinding()] param ( [Parameter(Mandatory=$true)] [string] $MyInput ) function Show-Input
I have the following PowerShell code: function Get-SmoConnection { param ([string] $serverName = ,
I have a function in powershell 2.0 named getip which gets the IP address(es)
I have a powershell function that takes a parameter like this: function whatever {
I have some code in a powershell function that looks like this: try {
I have this PowerShell cmdlet: function Test-ParameterBinding { # # .SYNOPSIS # Tests parameter
I have this code in one of my PowerShell scripts: function callCommandWithArguments([String] $arg1, [String]
I have a function defined as follows: function Get-LatestProjects { [CmdletBinding()] Param ( [Parameter(ValueFromPipeline
I have a function in a powershell script that is supposed to untar my

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.