# launching powershell script from cmake

**URL:** https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844
**Category:** Usage
**Created:** [April 6, 2023, 4:28pm UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844 "2023-04-06T16:28:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 6, 2023, 4:28pm UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/1 "2023-04-06T16:28:46Z")

</div>

Hi,

This question is linked with [#7833](https://discourse.cmake.org/t/execute-process-for-detecting-bash-issues-with-grep-and-awk-commands-arguments/7833).

I designed a powershell script that can take a path from CLI and add it to user path. I can run the script from a powershell or a cmd terminal but not from CMake.  
I tried:

```auto
execute_process(COMMAND "powershell -File <path to script>/AddUserPath.ps1 -NewPath NewPath")
execute_process(COMMAND powershell "-File <path to script>/AddUserPath.ps1 -NewPath NewPath")
execute_process(COMMAND powershell -File <path to script>/AddUserPath.ps1 -NewPath NewPath)

```

or without the -File

I get only errors or nothing (for the first case).  
the errors can be -File or /AddUserPath.ps1 not recognised as an applet, function or whatever.

Thanks by advance  
A.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 10, 2023, 1:17pm UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/2 "2023-04-10T13:17:54Z")

</div>

Hi,

I’m still stuck with this. Does anyone have example of powershell usage from CMake (whatever the running shell from which CMake is launched).

Thanks  
A.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 14, 2023, 5:01pm UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/3 "2023-04-14T17:01:45Z")

</div>

Hi,  
I found a syntax that works, at least on windows, without space in the path:

```auto
execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)

```

but if the path is containing space(s) I can’t find a way to run the script:

```auto
execute_process(COMMAND powershell Myself/my path/Scripts/Test.ps1 "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:/Users/Myself/my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell Myself/my path/Scripts/Test.ps1 "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:/Users/Myself/my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell Myself\my path\Scripts\Test.ps1 "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\Users\Myself\my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell "Myself\\my path\\Scripts\\Test.ps1" "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ C:/Users/Myself/my path/Scripts/Test.ps1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\Users\Myself\my:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
execute_process(COMMAND powershell "\"Myself\\my path\\Scripts\\Test.ps1\"" "-NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ ... \Desktop\de v\CompilationFramework\Scripts\Test.ps1" -NewPath NewPath
+ ~~~~~~~~
unexpected token "-NewPath"
execute_process(COMMAND powershell "\"Myself\\my path\\Scripts\\Test.ps1\" -NewPath NewPathFromCMake2" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER)
+ ... \Desktop\de v\CompilationFramework\Scripts\Test.ps1" -NewPath NewPath
+ ~~~~~~~~
unexpected token "-NewPath"

```

is it a bug of execute\_process?  
how can I write the call properly?

Thanks  
A.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 14, 2023, 10:38pm UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/4 "2023-04-14T22:38:09Z")

</div>

Use quotes around the path to your script. CMake will then ensure the shell sees it as a single argument.

```cmake
execute_process(
    COMMAND powershell
        "Myself/my path/Scripts/Test.ps1"
        "-NewPath NewPathFromCMake2" # This looks suspicious, should it really be a single argument?
    OUTPUT_VARIABLE FOO
    RESULT_VARIABLE FOOR
    ERROR_VARIABLE FOO_ER
)

```

Note that the quotes you use are not passed through to the shell command. They are interpreted by CMake as part of parsing the call to `execute_process()`. When you quote something like `-NewPath NewPathFromCMake2`, you are telling CMake to keep that together as a single argument on the command line. CMake will then apply quoting for you if needed, such as when there’s an embedded space or other special shell character.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [April 15, 2023, 10:33am UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/5 "2023-04-15T10:33:43Z")

</div>

Dear Craig,

I’m sorry but I still does not work. I tried to be a bit more exhaustive in my tests:

```auto
execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell C:/Users/Myself/mypath/Scripts/Test.ps1 -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\"" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR unexpected token -NewPath
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\"" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR unexpected token -NewPath
execute_process(COMMAND powershell "C:/Users/Myself/mypath/Scripts/Test.ps1 -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) SUCCESS
execute_process(COMMAND powershell "\"C:/Users/Myself/mypath/Scripts/Test.ps1\" -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR unexpected token -NewPath

execute_process(COMMAND powershell C:/Users/Myself/my path/Scripts/Test.ps1 "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell C:/Users/Myself/my path/Scripts/Test.ps1 -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\"" "-NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\"" -NewPath NewPath OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path
execute_process(COMMAND powershell "C:/Users/Myself/my path/Scripts/Test.ps1 -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR C:/Users/Myself/my is not recognised as a command applet
execute_process(COMMAND powershell "\"C:/Users/Myself/my path/Scripts/Test.ps1\" -NewPath NewPath" OUTPUT_VARIABLE FOO RESULT_VARIABLE FOOR ERROR_VARIABLE FOO_ER) ERROR "C:/Users/Myself/my;path/Scripts/Test.ps1" unexpected token -NewPath: note the appearance of the semi-column within the path

```

Yet I made experiments directly within windows shell and there is multiple issues.  
the right way to call a script seems to depend on the calling shell, and a ``` must be used in order to escape the whitespace:  
in native shell (cmd) I found these syntaxes:

```auto
powershell C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

```

or

```auto
powershell - Command C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

```

in powershell I found two possibilities

```auto
powershell -File C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

```

or

```auto
C:/Users/Myself/my` path/Scripts/Test.ps1 -NewPath NewPathValue

```

The easy part will be, with string(replace…) to change all space by `space.  
Nevertheless, I don’t know in what shell my command is launch from CMake. I suspect that both situation may arise but I don’t know how to determine in what shell the command will be launched.  
How can I do?  
Or is there another syntax that will works whatever the shell?

By the way, I have another question. My script returns with EXIT and a value.  
Nethertheless the returned value retrieve by execute process is always 0 when return is 0 but 1 whatever the returned value if its not 0. Is it a normal behavior?

Thanks.  
A.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 24, 2023, 4:37am UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/6 "2023-04-24T04:37:45Z")

</div>

It seems like there’s probably some `cmd /c` getting in the way here (of quoting and return codes)? I don’t see it in the source though. I wonder if `VERBATIM` might help here too?

---

<div class="post-metadata">

### Author: ![MarkSchofield](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@MarkSchofield](https://discourse.cmake.org/u/MarkSchofield)
#### Post date: [July 12, 2024, 6:06am UTC](https://discourse.cmake.org/t/launching-powershell-script-from-cmake/7844/7 "2024-07-12T06:06:15Z")

</div>

I have a file - [PowerShell.cmake](https://github.com/MarkSchofield/WindowsCMake/blob/main/WindowsCMake/PowerShell.cmake) - that implements:

```cmake
        execute_powershell(
            <script>
            [OUTPUT_VARIABLE <variable name>]
            [RESULT_VARIABLE <variable name>]
        )

```

Which allows you to run ‘inline’ scripts which would allow you to also run .ps1 files, too. I use it - for example - for xml parsing files on Windows, like this (taken from [here](https://github.com/MarkSchofield/WindowsCMake/blob/74157ac8129681eebf7b124e57ff0c8338ad61d9/WindowsCMake/MidlRT.cmake#L38C1-L47C2)):

```cmake
# Read the PLATFORM_REFERENCES from the WINDOWS_KITS_PLATFORM_PATH file.
execute_powershell("
$ErrorActionPreference = 'Stop'
[xml]$Platform = Get-Content \"${WINDOWS_KITS_PLATFORM_PATH}\"
$Platform.ApplicationPlatform.ContainedApiContracts.ApiContract |
    ForEach-Object { $_.name, $_.version }
"
    OUTPUT_VARIABLE PLATFORM_REFERENCES
    RESULT_VARIABLE POWERSHELL_RESULT
)

```
