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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:04:13+00:00 2026-06-11T11:04:13+00:00

I would like -h without passing a value to work as well as -h

  • 0

I would like -h without passing a value to work as well as -h with a parameter:
However, I would like the others to remain how they are… That is, they are optional, unless you alias a parameter and you are required to supply a value.

Param(
$BadParam,
[alias("h")][String]$Help,
[alias("s")][String]$Server,    
[alias("d")][String]$DocRoot,
[alias("c")][String]$Commit,
[alias("b")][String]$Branch
)

For example (for lack of better):

if(!$Help)
{
Write-Host "Here's the menu for how it works bro."
}

if($Help)
{
Write-Host "Here's more info on that particular thing from the menu."
}

How I would expect this to behave in shell:

-> script.ps1 -s test -h
-> Here's the menu for how it works bro.

-> script.ps1 -s test -h server
-> Here's more info on that particular thing from the menu.

-> script.ps1 -s -h server
-> You are missing a value for -e.
  • 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-11T11:04:14+00:00Added an answer on June 11, 2026 at 11:04 am

    Powershell doesn’t really support something like this. You can have a [switch] parameter which can be specified with no value, or you can have a [string] parameter which must either be completely absent, or provided with a value. You can’t have a parameter which supports being absent, specified with no value, and specified with a value.

    One alternative is the make it a [string], but have users pass $null, but that kind of sucks.

    Another extremely hacky alternative is the do some trickery with parameter positions. -Help could be a [switch], then there could be another parameter -ThingHelp positioned right after it which is a [string]. Then an invocation which looks like -Help 'foo' is not setting $Help to 'foo' but rather setting the $Help switch, and also assigning $ThingHelp = 'foo'.

    function ParamTest
    {
      param(
         [Parameter(Position = 0)]
         [switch]$Help,
    
         [Parameter(Position = 1)]
         [string] $ThingHelp
    
      )
    
       "Help is [$help]"
       "ThingHelp is [$thingHelp]"
    }
    

    This supports the following:

    PS> ParamTest               # $help = $false, $thingHelp = null
    PS> ParamTest -Help         # $help = $true, $thingHelp = null
    PS> ParamTest -Help 'foo'   # $help = $true, $thingHelp = 'foo'
    

    Hacks like this are not a good idea in general, since they tend to be both fragile and confusing. You should be able to give you scripts to an average powershell user, and they should immediately understand the meaning and usage of your parameters. If you have to explain a bunch of custom stuff that none of the thousands of other cmdlets out there are doing, you’re probably wasting everyone’s time.

    And if you are literally implementing some kind of help/usage system, please stop! Powershell provides a comprehensive help system for free. You can document every parameter, give example code, etc, without anything extra. See documentation for comment-based help.

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

Sidebar

Related Questions

I have an existing XML file that I would like to append without changing
Here is my test code that I would like to work. function test() {
I would like to remove all trailing zeros without truncating or rounding the number
I would like to run my node.js application without the automatic logs it prints
I would like copy just file.xml without folder structure using overlays like this: <overlays>
I would like to clone a tag using Javascript (without using any external frameworks
I would like to update a number without changing its placement in the output
I would like to use the following class across multiple modules without needing log
I would like to make my drivers to install on x64 Windows without the
I would like to update (change the content) some part of the webpage without

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.