After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell?
No. Any legacy commands invoked from PowerShell will run in a separate (child) process.
Proof:
wmic process where "name='powershell.exe' or name='cmd.exe'" get CommandLine, name, ParentProcessId, ProcessId /Value
Add above line to your batch-file, e.g. to setupEnv.bat
and call it from powershell
- either directly, e.g.
D:\bat\setupEnv.bat
,
- or using
&
call operator & D:\bat\setupEnv.bat
,
- or
.
dot sourced & D:\bat\setupEnv.bat
,
- or modify all above methods calling
cmd
as
cmd /D /C D:\bat\setupEnv.bat
, or
& cmd /D /C D:\bat\setupEnv.bat
, or
. cmd /D /C D:\bat\setupEnv.bat
.
Result is always the same or at least very alike:
PS D:\PShell> D:\bat\setupEnv.bat
"Setup Completed"
CommandLine="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Name=powershell.exe
ParentProcessId=4280
ProcessId=6396
CommandLine=C:\Windows\system32\cmd.exe /c ""D:\bat\setupEnv.bat""
Name=cmd.exe
ParentProcessId=6396
ProcessId=4116
Paraphrased from this Foredecker's answer to similar question:
While a child process can inherit the current environment variables,
working directory etc. from parent one, there is no supported way for
a child process to reach back to the parent process and change the
parent's environment.
Solution: call Powershell from cmd
/batch script rather than vice versa