AntiMalware Status

Setting a system environment variable from command line in Windows Server 2012 R2

Are you asking if it is possible to set a environment variable at the system level from a command prompt in [easyazon_link identifier=”178439307X” locale=”US” tag=”wn0d5-20″]Windows Server 2012 R2[/easyazon_link]? The answer is YES. I am for example trying to set system variables for my Amazon ACCESS and SECRET key so I can utilize the Amazon CLI tools. You can add User or System variables via GUI:

  • Press “Windows-Key+X” and select  System
  • Select Advanced System Properties
  • Select Environment Settings
  • Now you can add User or System variables

Other way is to use simple command line and save few click for your self 🙂

  • Open CMD.exe and run as Administrator

To add user variable such as JAVA_HOME=C:\Program Files (x86)\Java\jre

setx JAVA_HOME "C:\Program FIles (x86)\Java\jre"

To add system variables you have to use switch -m in the command line

setx -m AMAZON_ACCESS_KEY "012343456789"
setx -m AMAZON_SECRET_KEY "vmadvgjhgsdjgsdiiuuygdisggksgd"

If you check the variables via GUI path – you will see that these variables are added in the correct place. I am sure now you think that this can be scripted – yes it can. Easily you can create a batch file with those variables and run as Administrator to apply them into your system.

About the utility – you can find it in [easyazon_link identifier=”1449371620″ locale=”US” tag=”wn0d5-20″]Windows 8[/easyazon_link], [easyazon_link identifier=”0735682550″ locale=”US” tag=”wn0d5-20″]Windows Server 2012 R2[/easyazon_link]:

C:\setx /?
Description:
    Creates or modifies environment variables in the user or system
    environment. Can set variables based on arguments, regkeys or
    file input.
-
NOTE: 1) SETX writes variables to the master environment in the registry.
      2) On a local system, variables created or modified by this tool
         will be available in future command windows but not in the
         current CMD.exe command window.
      3) On a remote system, variables created or modified by this tool
         will be available at the next logon session.
      4) The valid Registry Key data types are REG_DWORD, REG_EXPAND_SZ,
         REG_SZ, REG_MULTI_SZ.
      5) Supported hives:  HKEY_LOCAL_MACHINE (HKLM),
         HKEY_CURRENT_USER (HKCU).
      6) Delimiters are case sensitive.
      7) REG_DWORD values are extracted from the registry in decimal
         format.
Examples:
    SETX MACHINE COMPAQ
    SETX MACHINE "COMPAQ COMPUTER" /M
    SETX MYPATH "%PATH%"
    SETX MYPATH ~PATH~
    SETX /S system /U user /P password  MACHINE COMPAQ
    SETX /S system /U user /P password MYPATH ^%PATH^%
    SETX TZONE /K HKEY_LOCAL_MACHINE\System\CurrentControlSet\
         Control\TimeZoneInformation\StandardName
    SETX BUILD /K "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
         NT\CurrentVersion\CurrentBuildNumber" /M
    SETX /S system /U user /P password TZONE /K HKEY_LOCAL_MACHINE\
         System\CurrentControlSet\Control\TimeZoneInformation\
         StandardName
    SETX /S system /U user /P password  BUILD /K
         "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\
         CurrentVersion\CurrentBuildNumber" /M
    SETX /F ipconfig.out /X
    SETX IPADDR /F ipconfig.out /A 5,11
    SETX OCTET1 /F ipconfig.out /A 5,3 /D "#$*."
    SETX IPGATEWAY /F ipconfig.out /R 0,7 Gateway
    SETX /S system /U user /P password  /F c:\ipconfig.out /X

Have one with this utility !

[easyazon_image align=”none” height=”160″ identifier=”1449342574″ locale=”US” src=”http://blog.technotesdesk.com/wp-content/uploads/2015/05/51emIPDVFDL._SL160_.jpg” tag=”wn0d5-20″ width=”122″]

Leave a Reply