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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:56:00+00:00 2026-05-13T14:56:00+00:00

What is the best way to handle command-line arguments? It seems like there are

  • 0

What is the “best” way to handle command-line arguments?

It seems like there are several answers on what the “best” way is and as a result I am stuck on how to handle something as simple as:

script.ps1 /n name /d domain

AND

script.ps1 /d domain /n name.

Is there a plugin that can handle this better? I know I am reinventing the wheel here.

Obviously what I have already isn’t pretty and surely isn’t the “best”, but it works.. and it is UGLY.

for ( $i = 0; $i -lt $args.count; $i++ ) {
    if ($args[ $i ] -eq "/n"){ $strName=$args[ $i+1 ]}
    if ($args[ $i ] -eq "-n"){ $strName=$args[ $i+1 ]}
    if ($args[ $i ] -eq "/d"){ $strDomain=$args[ $i+1 ]}
    if ($args[ $i ] -eq "-d"){ $strDomain=$args[ $i+1 ]}
}
Write-Host $strName
Write-Host $strDomain
  • 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-05-13T14:56:00+00:00Added an answer on May 13, 2026 at 2:56 pm

    You are reinventing the wheel. Normal PowerShell scripts have parameters starting with -, like script.ps1 -server http://devserver

    Then you handle them in a param section (note that this must begin at the first non-commented line in your script).

    You can also assign default values to your params, read them from console if not available or stop script execution:

     param (
        [string]$server = "http://defaultserver",
        [Parameter(Mandatory=$true)][string]$username,
        [string]$password = $( Read-Host "Input password, please" )
     )
    

    Inside the script you can simply

    write-output $server
    

    since all parameters become variables available in script scope.

    In this example, the $server gets a default value if the script is called without it, script stops if you omit the -username parameter and asks for terminal input if -password is omitted.

    Update:
    You might also want to pass a "flag" (a boolean true/false parameter) to a PowerShell script. For instance, your script may accept a "force" where the script runs in a more careful mode when force is not used.

    The keyword for that is [switch] parameter type:

     param (
        [string]$server = "http://defaultserver",
        [string]$password = $( Read-Host "Input password, please" ),
        [switch]$force = $false
     )
    

    Inside the script then you would work with it like this:

    if ($force) {
      //deletes a file or does something "bad"
    }
    

    Now, when calling the script you’d set the switch/flag parameter like this:

    .\yourscript.ps1 -server "http://otherserver" -force
    

    If you explicitly want to state that the flag is not set, there is a special syntax for that

    .\yourscript.ps1 -server "http://otherserver" -force:$false
    

    Links to relevant Microsoft documentation (for PowerShell 5.0; tho versions 3.0 and 4.0 are also available at the links):

    • about_Scripts
    • about_Functions
    • about_Functions_Advanced_Parameters
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What's the best way to grab/parse command line arguments passed to a
Possible Duplicate: Best way to parse command line arguments in C#? What's the best
Say that I have simple command line application, that looks something like this #
what is the best way to handle big integers in expr command. we know
What is the best way to handle unexisting actions in the Zend Framework? Depending
What's the best way to handle an expired password in an ASP.NET MVC application?
What is the best way to handle multiple Flex/Bison parsers inside a project? I
What is the best way to handle mysql database users connection in PHP? I
What's the best way to handle memory management with nested factory methods, such as
What is the best way to handle following situation? A dropdown(for master table) is

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.