In powershell after changing to cmd unable to call a bat file through a batch script file


I created a bat file to setup my workspace by changing the directory to the workspace directory and calling the setupEnv.bat file. But while I'm executing the below bat file in PowerShell, the instructions after <em>cmd</em> are not executing. I need to call the setupEnv.bat file in cmd. If I remove the <em>cmd</em> it will work fine. But I want call the setupEnv.bat on cmd not in PowerShell.

D: cd D:\WorkSpace\ cmd call setupEnv.bat echo "Setup Completed" <ul><li>After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell ?</li> </ul>


This article addresses your exact scenario:

<a href="http://windowsitpro.com/powershell/take-charge-environment-variables-powershell" rel="nofollow">Windows IT Pro - Take Charge of Environment Variables in PowerShell</a>

The reason the variables disappear is that a .bat or .cmd runs in a separate cmd.exe process (when the process terminates, you lose the variables).

The article presents a PowerShell function you can use to run a .bat or .cmd script and retain the environment variables it sets:

# Invokes a Cmd.exe shell script and updates the environment. function Invoke-CmdScript { param( [String] $scriptName ) $cmdLine = """$scriptName"" $args & set" & $Env:SystemRoot\system32\cmd.exe /c $cmdLine | Select-String '^([^=]*)=(.*)$' | ForEach-Object { $varName = $_.Matches[0].Groups[1].Value $varValue = $_.Matches[0].Groups[2].Value Set-Item Env:$varName $varValue } }

The article also has a couple of functions for setting and restoring environment variable values in PowerShell.


try Something like this into your PowerShell script:

& start "C:\Temp\test.bat"


<strong>After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell?</strong>

No. Any legacy commands invoked from PowerShell will run in a separate (child) process.


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

<ul><li>either directly, e.g. D:\bat\setupEnv.bat,</li> <li>or using <a href="https://ss64.com/ps/call.html" rel="nofollow">& call operator</a> & D:\bat\setupEnv.bat, </li> <li>or <a href="https://ss64.com/ps/source.html" rel="nofollow">. dot sourced</a> & D:\bat\setupEnv.bat, </li> <li>or modify all above methods calling cmd as <ul><li>cmd /D /C D:\bat\setupEnv.bat, or</li> <li>& cmd /D /C D:\bat\setupEnv.bat, or</li> <li>. cmd /D /C D:\bat\setupEnv.bat.</li> </ul></li> </ul>

Result is always the same or at least very alike:

<pre class="lang-none prettyprint-override">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 <a href="https://stackoverflow.com/a/774102/3439404" rel="nofollow">this Foredecker's answer to similar question</a>:


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.


<strong>Solution</strong>: call Powershell from cmd/batch script rather than <a href="http://www.dictionary.com/browse/vice-versa?s=t" rel="nofollow">vice versa</a>


  • how to solve error of jsp page?
  • pass variables as array specflow c#
  • Is there a TPL dataflow block that doesn't take input but returns output?
  • Gurobi reports unbounded model despite mathematical impossibility
  • Mysql: How can I use RTRIM in my LOAD DATA INFILE query?
  • UML Sequence Diagram Message Branching
  • Why my input is showing one thread executing after another thread, not at the same time?
  • XCB equivalent of XkbSetDetectableAutoRepeat
  • how do you “Press ENTER to continue, anything else to quit” in C++
  • How can I get a specific version of a dataset row?
  • FragmentActivity with a Fragment Containing a MapView
  • Always require certain dependencies in RequireJS
  • Slicing an SPA into several components and use AngularJS
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • ASP.NET windows authentication should always ask for credentials
  • Is there any purpose for h2-h6 headings in HTML5?
  • Lua: Line breaks in strings
  • Python pandas melting data to multiple columns and coulmn names in another column
  • Facebook iOS SDK Not Calling Completion Handler
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • How can I replace the server in Web Component Tester
  • aapt.exe'' finished with non-zero exit value 1
  • Tamper-proof configuration files in .NET?
  • Code in Job's Script Block after Start-Process Does not Execute
  • How can I sort a a table with VBA with given text condition?
  • Fetching methods from BroadcastReceiver to update UI
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Running a C# exe file
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Proper folder structure for lots of source files
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?