1

I want to run multiple separate commands in the same instance of PowerShell using C++.

The first command would be cding into a folder containing a python venv.

cd C:\\FolderPath

The second command would be ./Scripts/activate to run the virtual environment, with more commands possible later in the code.

I am aware of using ";" to separate commands in PowerShell, but for my purposes I would like to have separate command sets. For example something like:

auto cmdInstanceStore = GetCMDInstance(system("PowerShell"));
system("cd C:\\FolderPath", cmdInstanceStore); //Passing cmdInstanceStore runs the cd C:\\FolderPath command in the same cmd in which PowerShell command ran
system(".\Scripts\activate, cmdInstanceStore); //Passing cmdInstanceStore runs the .\Scripts\activate command in the same cmd in which PowerShell and cd command ran

//run more cpp code//
system("command 1", cmdInstanceStore); //Same idea as before, command 1 would run in the same cmd as  previous commands (command 1 would have access to env variable in the virtual environment activated earlier)

I am on Windows 10. Any help would be appreciated, thanks.

DSByte
  • 11
  • 2
  • 1
    You need to launch a `powershell.exe` process with `-c -`, then you can feed commands to it via stdin; [this answer](https://stackoverflow.com/a/69036873/45375) may get you started. – mklement0 Nov 30 '21 at 04:11
  • 1
    @mklement0 that was useful, thanks. – DSByte Nov 30 '21 at 17:18

0 Answers0