# Setting environment variables in a batch file called using execute\_process.

**URL:** <https://discourse.cmake.org/t/setting-environment-variables-in-a-batch-file-called-using-execute-process/4861>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [January 16, 2022, 1:21pm UTC](https://discourse.cmake.org/t/setting-environment-variables-in-a-batch-file-called-using-execute-process/4861 "2022-01-16T13:21:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![underdisc](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/858c86/32.png) [@underdisc](https://discourse.cmake.org/u/underdisc)\
**Post date:** [January 16, 2022, 1:21pm UTC](https://discourse.cmake.org/t/setting-environment-variables-in-a-batch-file-called-using-execute-process/4861/1 "2022-01-16T13:21:46Z")

</div>

Say I have a batch file that looks like this.

```auto
set varA=valueA

```

I want my CMakeLists.txt file to make use of this environment variable with the syntax provided by CMake.

```auto
$ENV{varA}

```

Before that can be done, I need to run the batch script to set that environment variable. So, I run it using `execute_process` like so.

```auto
execute_process(COMMAND var.bat)
# varA should now be set to the value specified in var.bat.
add_executable($ENV{varA})

```

_This does not work_. Running the batch script using `execute_process` does not update the environment variable. Is it necessary to run the batch script before invoking CMake or is there something I am missing that makes this work?

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [January 16, 2022, 5:27pm UTC](https://discourse.cmake.org/t/setting-environment-variables-in-a-batch-file-called-using-execute-process/4861/2 "2022-01-16T17:27:03Z")

</div>

`execute_process` starts a new process that sets the environment variable for itself. There’s no way for the child process to set environment variables for the parent (which is what you’re trying to do here).

So yes - you have to set the environment before running CMake.

Other possibility would be for your bat script to print the desired environment variables so that you could capture that output and process however you want.
