[Solved] Would like to run PowerShell code from inside a C++ program

I want to be able to run like 30 lines of PowerShell script from my c++ program. I’ve heard it’s a terrible idea, I don’t care. I still would like to know how.

I just want to code directly in the C++ program, i do not want to externally call the PowerShell script. Is there any way to do this? If it’s impossible, just say no.

For example

void runPScode() {

//command to tell compiler the following is PowerShell code
//a bunch of PowerShell code

}

Thanks!

I’ve looked for commands to do this and have read several ‘similar’ questions.

Solution #1:

//command to tell compiler the following is PowerShell code

No! There’s no such command to tell the compiler, and

//a bunch of PowerShell code ...

being executed inline.

You can achieve this using the CreateProcess() function choosing your shell, and provide it with the appropriate code to execute.

Respondent: ????? ???

Solution #2:

This just for completeness’ sake:


PowerShell has an API – see System.Management.Automation.PowerShell. The API is managed (i. e. .NET-based). It’s possible to build a mixed mode C++ application and invoke said API from the managed part.

Place the following into a separate C++ file:

#include "stdafx.h"
#include <vcclr.h>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Management.Automation.dll>

using namespace System;
using namespace System::Management::Automation;

void RunPowerShell(LPCWSTR s)
{
    PowerShell::Create()->AddScript(gcnew String(s))->Invoke();
}

In Project Properties, under VC++ Directories, add C:Program Files (x86)Reference AssembliesMicrosoftWindowsPowerShell3.0 to Reference Directories (your path may vary).

Set the following compiler options for that file only:

  • Common Language RunTime Support (/clr)
  • Debug information format – Program Database (/Zi)
  • Enable C++ exceptions – No
  • Basic runtime checks – Default
  • Precompiled header – Not Using Precompiled Headers

You need /clr for calling .NET from C++, but /clr is incompatible with a bunch of other C++ options. If you miss something, the compiler error messages will let you know.

Declare void RunPowerShell(LPCWSTR) as a regular external function in the unmanaged parts of the project, invoke as needed.


That said, whatever your Powershell does, C++/Win32 probably can do too.

Respondent: Seva Alekseyev

Solution #3:

You have two options: using system or CreateProcess. The system documentation is found at:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2019

Using this method you pass a string command. An example, as shown in the documentation:

system( "type crt_system.txt" );

CreateProcess documentation is found at:

https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa

Using this command is a little trickier and I wouldn’t recommend it for simple commands.

For additional information please see:
how can we use a batch file in c++?

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.