Question

set a system environment variable from a batch command file

Asked by: mordi

What is the command to set a system environment variable from a batch command file ?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-07-26 at 03:35:35ID21932236
Tags

environment

,

variable

Topic

Windows 98 Operating System

Participating Experts
2
Points
100
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. environment variables
    i am not sure how environment variables work - i have few questions @ it. . 1. where they are stored 2. if i run command prompt -cmd.exe in windows XP and in that command prompt if i change environment variables value for e.g. if oracle_home is set to d:\oracle9i...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: callrsPosted on 2006-07-26 at 04:06:44ID: 17182915

set

e.g.
set PATH=D:\usr\bin\;%PATH%;"D:\E\HEX\010EDI~1"

Then to use the variable e.g. in an IF statement, you have to use this syntax: %PATH%


 

by: callrsPosted on 2006-07-26 at 04:07:23ID: 17182919

http://www.computerhope.com/sethlp.htm     MS-DOS set command help

 

by: BillDLPosted on 2006-07-26 at 07:53:19ID: 17184763

mordi, what exactly is it that you need to do with your batch file.
Knowiing these details we may be able to offer specific advice.

You should be aware that, if you just set a variable in a batch file that you then run from within Windows, it will only be held in memory for the duration of that batch file.  If used from Full DOS, it will only last for the duration of that session.  Set from somewhere like C:\Autoexec.bat, the variable should remain set for the duration of the Windows session.

Type the single command   SET   in a DOS Window to see the environment variables currently set on your system.

There is another utility file named WINSET.EXE from which you can set Windows environment variables that outlasts the batch file from which the command was run, and should remain for the duration of the Windows session.

Let us know what you are trying to do.

 

by: mordiPosted on 2006-07-26 at 08:06:05ID: 17184919

I am talking about system environment variables.Do you know what they are?

 

by: callrsPosted on 2006-07-26 at 12:06:52ID: 17187124

http://www.wilsonmar.com/1envvars.htm     Environment Variables
But XP, not 98, has more pre-defined ones like %date% and %time%:
http://kennethhunt.com/archives/000933.html     Environment Variables Windows 2000/XP : Tech Observer
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx  XP - Command shell overview

 

by: BillDLPosted on 2006-07-27 at 06:56:10ID: 17192713

mordi.
Yes, I do know. System environment variables are for global use and User environment variables are for the logged on user.  The latter is mostly applicable to Windows NT-based systems or multi-user logons.

Here's some extracts of notes I previously made a while ago which you may find useful.

When setting a path in autoexec.bat or another batch file, I'm sure that there is a maximum limit of 127 or 128 characters.  As I understand it, setting the path from config.sys does not have this limitation.

The Win9x Environment Space is limited, and you could easily exceed this and see an error message "Out of Environment Space".
You can increase the space by adding the following lines:

C:\CONFIG.SYS:
SHELL=C:\WINDOWS\COMMAND.COM /P /E:4096

C:\Windows\SYSTEM.INI under the [NonWindowsApp] section header:
CommandEnvSize=2424

If you set a variable that comprises a path containing spaces
eg: set BILLSPICS=C:\Windows\Profiles\Bill\My Documents\My Pictures
and you want to use the %BILLSDOCS% variable in a batch file, then enclose it in " " like this:
DIR /on /s "%BILLSPICS%\*.jpg" > %TEMP%\BillsJPGs.txt
Preferably, use the DOS names for variables using paths with spaces
eg: set BILLSPICS=C:\Windows\Profiles\Bill\MYDOCU~1\MYPICT~1

If an environment variable contains a directory name ending with a trailing backslash (\)
eg: set dir=c:\folder1\
COMMAND.COM will not accept the CD (change directory) command using the specified directory held in the variable. Avoid using a trailing \ in path variables.

If you redirected some program output that appends a \ to a path, and then tried to SET that as a variable, you COULD HAVE stripped off the trailing backslash on a Windows NT-based system using the following command, but I don't think there is a way in Windows 98:
set STRING=%1
if "%STRING:~-1%"=="\" set STRING=%STRING:~0,-1%

PATH TIP:
To clear all search-path settings other than current directory), use the following syntax:
PATH ;

You shouldn't have to use anything other than autoexec.bat, config.sys, and msdos.sys to set system environment variables, but if you do come upon any limitations then look at utility programs like this:

http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c10849/

WINSET.EXE
----------

First of all, beware of rogue versions of Winset.exe:
http://www.pcreview.co.uk/startup/winset.exe.php

Winset Uses:

By Al Fasoldt:
http://aroundcny.com/Technofile/texts/bkwinset98.html

By Eric Phelps:
Winset lets you set environment variables globally (they persist once your batch file ends).
ftp://ftp.microsoft.com/Products/Windows/Windows95/CDRomExtras/AdministrationTools/ApplicationTools/envars.exe
(self extracting .exe - unpack it to its own folder).
Note: Winset cannot be run from FULL DOS.

Other Notes (not sure if they are applicable to Windows 98 as well as 95):
The logon script processor for Windows NT logon scripts does not support setting environment variables in Windows 95. WINSET.EXE is a Win32 console application that overcomes this limitation. When called from a batch file (including an NT Logon script) it sets an environment variable in Window's global environment. Please note that WINSET.EXE does not set an environment variable in the batch file's environment, only Window's global environment. The syntax for WINSET.EXE is the same as the SET command in DOS, with one exception. It won't list all the environment variables set as you would see if you typed the SET command with no parameters.

Bill

 

by: mordiPosted on 2006-07-27 at 06:59:56ID: 17192741

I need to change  the system environment variable several times from my command , not only on system startup

 

by: BillDLPosted on 2006-07-28 at 00:17:15ID: 17198884

My information doesn't only refer to setting the variables at system startup.  That's the reason I mentioned the fact that the SET command run from a batch file within Windows will only last while that DOS window is open, or any other DOS window if the variable has been passed on to it.

That's also the reason I mentioned winset.exe ie. as a way to set environment variables that persist beyond the term of the batch file that called it.

You will find winset.exe in the ..\tools\reskit\scrpting  folder of a Win98se CD.  The file's properties describe the program file as "WINSET - Sets variables in the Windows 95 master environment".  Yes, I know it says "Windows 95", but it works in Windows 98/98se or it wouldn't be on the CD.

WINSET [variable=[string]]

variable  Specifies the environment-variable name.
string     Specifies a series of characters to assign to the variable.

Try it out and compare the results with the SET command when used from a batch file within Windows.
Get the batch file to set one variable using the SET command, and another variable using WINSET then exit.
Run a 2nd batch file and have it show the current environment variables using the SET command with no parameters.
The variable set using WINSET will have persisted, while the one created with the SET command will have been lost.
If you need to change the variable several times, then just clear the variable's value and reset it to another value.

TEST1.BAT - Run this first

@echo off
:: Clear any existing values for both variables
set VAR1=
set VAR2=
winset VAR1=
winset VAR2=
::
set VAR1=Created with Set (VAR1)
winset VAR2=Created with Winset (VAR2)
::
exit


TEST2.BAT - Run this after TEST1.BAT terminates and closes the DOS window

@echo off
echo.
echo Environment Variables shown by the 'SET' command:
echo.
set
echo.
echo Do you see VAR1 listed above?
echo No, because it was set using the 'SET' command.
echo VAR2, however, was set using the 'WINSET' command.
echo.
echo Now 'echoing' the variables ...
echo.
echo Value of VAR1: " %VAR1% " - Do you see anything?
echo Value of VAR2: "%VAR2%" - Bet you see this value!
echo.
echo press any key to end ...
pause>nul
:: Clear variables again before terminating
set VAR1=
set VAR2=
winset VAR1=
winset VAR2=
exit

OK, so maybe that doesn't quite do what you need, but try it anyway.

 

by: BillDLPosted on 2006-08-12 at 09:49:15ID: 17302664

mordi
Have you had the chance to test my suggestions out?

 

by: mordiPosted on 2006-09-13 at 01:07:10ID: 17509896

Sorry for thr delay.
I need it for windows XP and NOT for windows98, by mistake i entered the wrong os TOPIC.
What i need is as you said before, to set Windows environment variables that outlasts the batch file from which the command was run, and should remain for the duration of the Windows session.

 

by: BillDLPosted on 2006-09-14 at 06:53:29ID: 17520666

Hi mordi

set VARIABLENAME=Value

Related info:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/setlocal.mspx

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/environment_variables.mspx
http://support.microsoft.com/default.aspx?scid=kb;en-us;310519

1. Log on with  admin rights (any user can modify a "user" variable, but not a "system" one)
2. Right-click "My Computer" then click "Properties"
3. Click the Advanced tab
4. Click Environment variables
5. Options: "user" or "system" variable:
   (a) New variable - to add a new variable name and value <------- This one **
   (b) existing variable > Edit to change variable name or value
   (c) existing variable > Delete to remove an existing variable

http://vlaurie.com/computers2/Articles/environment.htm (With screenshots and additional info)

Windows, DOS, and other operating systems - interest value :
http://en.wikipedia.org/wiki/Environment_variable
http://www.wilsonmar.com/1envvars.htm

Hope this helps
Bill

 

by: BillDLPosted on 2006-09-14 at 06:55:31ID: 17520685

Sorry callrs, I ended up re-quoting links you provided earlier, but how did you know mordi was talking about Windows XP way back then? ;-)

 

by: mordiPosted on 2006-09-14 at 07:05:51ID: 17520746

Again it doesn't answer my question.
As i stated before, i need something like winset, to change a system variable from a batch file comand program.

 

by: BillDLPosted on 2006-09-14 at 22:21:31ID: 17526853

You still haven't given an example of a system variable you might need to change, so here's a method to change system or user environment variables from the command line.

Get your WinXP professional CD and navigate to the \SUPPORT\TOOLS\  folder.  Unzip the file SUPPORT.CAB to its own folder with WinZip, or use WinXP's own decompression shell extension to extract the following two files from SUPPORT.CAB:  setx.exe and suptools.chm.

The program file setx.exe is documented very briefly in the *.chm help file as follows:

>>>
This command-line tool offers administrators a batch method for setting environment variables in the user or system environment and requires no programming or scripting. In addition to taking an environment variable and its associated value from the command line, it can also get the values of registry keys and write them to text files.

You can use SetX to set values for user and system environment variables from one of three sources: Command Line Mode, Registry Mode, or File Mode.

SetX is similar to the UNIX utility SETENV.
SetX writes variables to the master environment in the registry.
Variables set by using SetX are available only in future command windows and not in the current command window.

SetX provides the only command-line or programmatic way to directly set system environment values for Windows XP. System environment variables can be manually configured by using Control Panel or by using Regedit. The Set command (which is part of the Windows operating system) only uses environment variables for the current console window.
<<<
 
You can redirect the syntax help from setx.exe by running the command:
setx /? > setx_syntax.txt
in the same folder as setx.exe.

I have detailed the output from this below

>>>
SETX:
This program is used to set values in the environment of the MACHINE or currently logged on USER using one of three modes.

1) Command Line Mode:
    setx variable value [-m]

    Optional Switches:
    -m  Set value in the Machine environment. Default is User.

2) Registry Mode:
    setx variable -k hive\key\...\value

    Optional Switches:
    -m  Set value in the Machine environment. Default is User.

3) File Mode: setx variable -f file {-a x,y | -r x,y "string"} [-d d] [-x] [-m]

    Required Switches:
    -f  file         : Specify file name to use.
    -a  x,y          : Specify absolute coordinates and offset.
    -r  x,y "string" : Specify coordinates and offset relative to string.

   Optional Switches
    -d  ,:\ etc.  : Specify additional delimiters.
    -x            : Displays file coordinates. Switches -a -r -e ignored!!
    -m            : Set value in the Machine environment. Default is User.
<<<

For more information and examples use: SETX -i (output as follows):

>>>
SETX Examples:
===========
For the file type examples you must first create the file that you wish to parse by using "command > filename" ie ipconfig > ipconfig.out.

IMPORTANT: SETX writes variables to the master environment in the registry. Variables set using SETX are only available in future command windows and not in the current command window.

SETX Command Line Examples:
--------------------------------------

SETX MACHINE COMPAQ
Sets value of MACHINE to be COMPAQ in the users environment.

SETX MACHINE "COMPAQ COMPUTER" -m
Sets value of MACHINE to be "COMPAQ COMPUTER" in the machine environment.

SETX MYPATH %PATH%
Sets the value of MYPATH to the CURRENT value of the PATH environment variable.

SETX MYPATH ~PATH~
Sets the value of MYPATH to ALWAYS be equal to the value of the PATH environment
variable even in the event that the PATH variable changes.

SETX Registry Examples:
------------------------------

SETX TZONE -k HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName
Sets the value of TZONE to the above key ie. "Central Standard Time"

SETX BUILD -k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber"
Sets the value of BUILD to the current Windows NT build ie. "1314"
Note: Quotes must be used because of the embedded space in "Windows NT".

SETX File Examples:
-------------------------

SETX VAR -f ipconfig.out -x
Displays the coordinates for the contents of the file "ipconfig.out".

SETX IPADDR -f ipconfig.out -a 5,11
Finds value at absolute offset 5,11 of the file ipconfig.out
Sets IPADDR to the IP Address (absolute offset 5,11)

SETX OCTET1 -f ipconfig.out -a 5,3 -d .
Finds value at absolute offset 5,3 and uses "." as an additional delimiter.
Sets OCTET1 to the first octet of the IP Address

SETX IPGATEWAY -f ipconfig.out -r 0,7 "Gateway"
Finds value at relative offset 0,7 to the keyword "Gateway"
Sets IPGATEWAY to the first octet of the IP Address
<<<

If you wish to know the registry keys that hold the environment variables, then export the following keys to *.REG or *.TXT files and you will see the preconfigured ones, plus any you or other programs may have added:

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment]

[HKEY_CURRENT_USER\Environment]

Hopefully this will help you do what you need to do, but if you need more info or perhaps some example CMD or BAT files then you'll get thousands of hits searching google.com for SETX.EXE

Bill

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...