I know that in the unix world, if you edit your .profile
or .cshrc
file, you can do a source ~/.profile
or source ~/.cshrc
to get the effect on your current session. If I changed something in the system variable on Windows, how can I have it effect the current command prompt session without exiting the command prompt session and opening another command prompt session?
8 Answers
In the usual Windows command prompt (i.e. cmd.exe), just using call mybat.bat
did what I wanted. I got all the environment variables it had set.

- 1,167
- 10
- 12
-
3If mybat.bat runs other batch scripts, it needs to `call` them to allow execution to return to the current script. – Eryk Sun Feb 03 '18 at 21:58
-
`call` won't work for powershell scripts however. Suppose you want to import a function and invoke it, `source` would work in bash – stelios Jan 24 '19 at 15:03
-
4This is a very old question, but I think this should be the correct answer. "call myfile.bat" --> reads the environment variables from the batch file and sets those in current environment from where "call" command is invoked. then all those variables are available for use in the current shell. – Chinmay Jun 11 '19 at 14:26
-
1Oh, and it was at a perfect 42... I almost wish I hadn't upvoted and just left it there. Thanks much! :) – coolaj86 Jul 18 '19 at 04:57
The dos shell will support .bat files containing just assignments to variables that, when executed, will create the variables in the current environment.
c:> type EnvSetTest.bat
set TESTXYZ=XYZ
c:> .\EnvSetTest.bat
c:> set | find "TESTX"
TESTXYZ=XYZ
c:>
IHTH.

- 36,525
- 7
- 83
- 90
-
@thomasa88: you can click the down vote button again to remove the down vote – BeniBela Feb 19 '13 at 11:53
-
1@BeniBela You can only undo the vote for a few minutes, then it gets locked. shellter: Vote corrected, thanks for the good answer :) Also found that setlocal can be used for local variables, and one can move data between local and global like this: http://wiki.answers.com/Q/In_a_DOS_batch_file_how_do_you_copy_a_local_variable_to_a_global_variable – thomasa88 Feb 19 '13 at 15:05
Following example will help you to solve your problem.
env.bat
This file is for setting variables. Its contents are given blow.
set name="test3"
test.bat
Our main batch file.
call env.bat
call print.bat
pause
Now print.bat
batch file to print variables. Its contents given below
echo %name%

- 8,401
- 4
- 40
- 42
I am afraid not, but you can start using Powershell, which does support dot sourcing. Since powershell window is really based on cmd so all your dos command will continue to work, and you gain new power, much more power.

- 3,734
- 1
- 21
- 17
-
Thanks John Shen. I have search the web and haven't found any equivalent. I have seen Powershell, perhaps I will start using that instead of the old command prompt. – mart2001 May 04 '12 at 16:45
-
14
-
4
-
"call" worked for me, as the second answer with more than 10 upvotes proves, too. – Frank Buss Sep 18 '20 at 07:44
The only way I have found this to work is to launch a new cmd window from my own config window. eg:
@echo off
echo Loading...
setlocal enabledelayedexpansion
call 1.cmd
call 2.bat
...
...
if "%LocalAppData%"=="" set LocalAppData=%UserProfile%\Local Settings\Application Data
SET BLAHNAME=FILE:%LocalAppData%\BLAH
call blah blah
cmd
The last cmd will launch a new cmd prompt with the desired settings exported to the command window.

- 997
- 12
- 14
Here's a workaround for some limited use-cases. You can read-in a file of commands and execute them in-line. For example the calling command file looks like:
echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:
echo. ----------------
echo. set-up java
echo. ----------------
echo.
rem call %DEV_SCRIPTS%\setup-java
for /F "tokens=*" %%A in ( %DEV_SCRIPTS%\setup-java.bat ) do (
%%A
)
call %DEV_SCRIPTS%\show-java
:
In the setup-java.bat
file you can't use %
expansion. You need to use !
; e.g.:
set JRE_HOME=!JRE_08!
rem
set JRE_TARGET=!JRE_HOME!
So you are litterally source
-ing commands from a text file. You will need to test which commands sourced in this way. It took a few trials just to set some environment variables.
I don't think we can do logic or loops because the command processor scans the file at the start. I am OK just having a simple workaround to reuse shared things like environment definitions. Most other things won't need an actual source
command (I am hoping). Good luck.

- 4,799
- 8
- 54
- 90
For example to set VC# vars
C:\Windows\System32\cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"

- 6,205
- 4
- 31
- 47

- 1