I need to create a windows batch script where I need to pass a few command-line arguments. These arguments may appear in in any order, but the script should pick them up properly based on the argument name. E.g.:

<scriptname> -age <agevalue> -gender <gender>
<scriptname> -gender <gender> -age <agevalue>

The script can be called in any of the two above ways but the age and gender should be properly picked up and assigned to variables. The arguments can be identified by the switch before them - i.e., -age , -gender, etc.

Can someone please give me an example of identifying this internally ?


batch, unfortunately, doesn't have a builtin getops function like does. However, you could implement your own poor-man's variant:

 if /I %~1 == --age set AGE=%2& shift
 if /I %~1 == --gender set GENDER=%2& shift
if not (%1)==() goto GETOPTS
  • this might be cumbersome as the number of arguments exceeds but it fits the bill. Thanks – Dhiwakar Ravikumar Sep 15 '14 at 5:42
  • 1
    Following robvanderwoude.com/if.php it would be better to write if /I "%~1" == "--age" ... (note the tilde). This also works for empty parameters or parameters which are put in double quotes. – Axel Kemper Sep 15 '14 at 7:29
  • You might want to add the double quotes. Otherwise, the statement does not work with empty parameter %1. – Axel Kemper Sep 15 '14 at 9:23
  • If your various possible arguments have a set format or type, such as age (being numeric) and gender (being "M" or "F") you shouldn't have to define the arguments, but instead use comparison recognition to assign them to the appropriate variables within the script. I did this for an early batch script I wrote during a transition period between 9x and XP at my first IT job. The script could either accept arguments for the OS type, the drive the script was running from, and a few other things, or it could auto-detect the appropriate settings. A bit overkill, but a fun exercise. – music2myear Oct 9 '14 at 15:48

Can you do it with Powershell? or does it need to be batch? Powershell makes it nice and easy, you just need to add something like this at the top of your script, and then you can call each parameter in any order

param (

You can do it with batch, but it will require lots of parameter checking.. Here's a few links of some batch scripts that do it


(Possible dupplicate of)


  • 1
    it has to be batch scripts only :(. We have a strict requirement that it be a batch script. Thanks a lot though :) – Dhiwakar Ravikumar Sep 15 '14 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.