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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:52:06+00:00 2026-06-11T10:52:06+00:00

I’m trying to create a powershell (2.0) script that will accept arguments that follow

  • 0

I’m trying to create a powershell (2.0) script that will accept arguments that follow this basic pattern:

.\{script name} [options] PATH

Where options are any number of optional parameters – think along the lines of ‘-v’ for verbose. The PATH argument will simply be whatever argument is passed in last, and is mandatory. One could call the script with no options and only one argument, and that argument would be assumed to be the Path. I’m having trouble setting up a parameters list that contains only optional parameters but is also non-positional.

This quick script demonstrates the problem I am having:

#param test script
Param(
    $firstArg,
    $secondArg,
    [switch]$thirdArg,
    [Parameter(ValueFromRemainingArguments = $true)]
    $remainingArgs)

write-host "first arg is $firstArg"
write-host "second arg is $secondArg"
write-host "third arg is $thirdArg"
write-host "remaining: $remainingArgs"

When called like so:

.\param-test.ps1 firstValue secondValue

The script outputs:

first arg is firstValue
second arg is secondValue
third arg is False
remaining:

The behavior I am trying to create would have both arguments fall through the optional params and end up in the remainingArgs variable.

This question/answer helpfully provided a way to achieve the desired behavior, but it only seems to work if there is at least one mandatory parameter, and only if it comes before all of the other arguments.

I can demonstrate this behavior by making firstArg mandatory and specifying a position of 0:

#param test script
Param(
    [Parameter(Mandatory=$true, Position = 0)]
    $firstArg,
    $secondArg,
    [switch]$thirdArg,
    [Parameter(ValueFromRemainingArguments = $true)]
    $remainingArgs)

    write-host "first arg is $firstArg"
    write-host "second arg is $secondArg"
    write-host "third arg is $thirdArg"
    write-host "remaining: $remainingArgs"

Run with the same input as before:

.\param-test.ps1 firstValue secondValue

The output is as follows:

first arg is firstValue
second arg is
third arg is False
remaining: secondValue

The first, mandatory argument is assigned, and everything left falls all the way through.

The question is this: How can I set up a params list such that all of the params are optional, and none of them is positional?

  • 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-11T10:52:07+00:00Added an answer on June 11, 2026 at 10:52 am

    How about this?

    function test
    {
       param(
          [string] $One,
    
          [string] $Two,
    
          [Parameter(Mandatory = $true, Position = 0)]
          [string] $Three
       )
    
       "One = [$one]  Two = [$two]  Three = [$three]"
    }
    

    One and Two are optional, and may only be specified by name. Three is mandatory, and may be provided without a name.

    These work:

    test 'foo'
        One = []  Two = []  Three = [foo]
    test -One 'foo' 'bar'
        One = [foo]  Two = []  Three = [bar]
    test 'foo' -Two 'bar'
        One = []  Two = [bar]  Three = [foo]
    

    This will fail:

    test 'foo' 'bar'
    test : A positional parameter cannot be found that accepts argument 'bar'.
    At line:1 char:1
    + test 'foo' 'bar'
    + ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [test], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,test
    

    This doesn’t enforce that your mandatory arg is placed last, or that it’s not named. But it allows for the basic usage pattern you want.

    It also does not allow for more than one value in $Three. This might be what you want. But, if you want to treat multiple non-named params as being part of $Three, then add the ValueFromRemainingArguments attribute.

    function test
    {
       param(
          [string] $One,
    
          [string] $Two,
    
          [Parameter(Mandatory = $true, Position = 0, ValueFromRemainingArguments = $true)]
          [string] $Three
       )
    
       "One = [$one]  Two = [$two]  Three = [$three]"
    }
    

    Now things like this work:

    test -one 'foo' 'bar' 'baz'
      One = [foo]  Two = []  Three = [bar baz]
    

    Or even

    test 'foo' -one 'bar' 'baz'
        One = [bar]  Two = []  Three = [foo baz]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.