Invalid syntax with setx

37,470

Solution 1

Your path to the Ogre SDK has a space character in it, which is interpreted as a delimiter to another argument. Surround your path with " to keep it as one single argument to setx:

setx OGRE_HOME "D:\Program Files\OgreSDK"

To see the current value of the OGRE_HOME environment variable:

echo %OGRE_HOME%

You may have to open a new command prompt shell to see the value if you set it and are then trying to immediately see it's value.

To see all currently set environment variables, simply run:

set

To show only environment variables that have a certain prefix (so FOO would show FOOBAR and FOOBAZ), put that prefix after set:

set PREFIX

Alternatively, you can use the GUI to edit environment variables (assuming Windows 7 here).

  • Right-click Computer, choose Properties
  • Click Advanced system settings in the left pane
  • Make sure you're on the Advanced tab in the pop-up dialog
  • Click Environment Variables... at the bottom

A dialog will pop up with your user-specific environment variables as well as your system-wide environment variables. Select a value and use the New/Edit/Delete buttons to interact with them.

Solution 2

Command Prompt is giving you that error because you forgot the quotation marks. You should’ve typed:

setx OGRE_HOME “D:\Program Files\OgreSDK”

To see all the values you’ve already set, enter either:

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

OR

reg query HKEY_CURRENT_USER\Environment

Solution 3

As an addendum to @ajp15243's answer. If you are doing the same with PowerShell rather than the command prompt or batch file, you'll need to call SETX with a leading escaped double-quote character, as in:

$my_path = "%PROGRAMFILES%\MySQL\MySQL Server 5.7\bin\"
$hkcu_path = (Get-ItemProperty hkcu:\Environment).PATH + ";" + $my_path
SETX PATH "`"$hkcu_path" # note the leading escaped quote

However doing so, may result in adding a trailing double quote in the value of hkcu:\Environment\PATH, so you may need to do this too:

$dirty_path = (get-itemproperty hkcu:\Environment).PATH
$clean_path = $dirty_path -replace '"',''
SETX PATH $clean_path
Share:
37,470
Aaron Lee
Author by

Aaron Lee

Updated on February 15, 2020

Comments

  • Aaron Lee
    Aaron Lee over 4 years

    I used the setx command to set OGRE_HOME:

    setx OGRE_HOME D:\Program Files\OgreSDK
    

    Now I need to change to value of OGRE_HOME. How can I search all the values I have set? If I run the command again, it shows that:

    ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
    
  • Aaron Lee
    Aaron Lee over 9 years
    Thank you very much. And I will never install software at path with space character, because it may catch some trouble that we are hard to check.
  • Andrew
    Andrew almost 7 years
    Note that you also need "" if you're using %PATH% (for appendages).