Featured image of post [VS2022] Execute PowerShell from C++! Integration Steps via C++/CLI DLL

[VS2022] Execute PowerShell from C++! Integration Steps via C++/CLI DLL

Host Windows PowerShell 5.1 from a C++/CLI DLL in Visual Studio 2022. Covers the matching .NET Framework target, reference assemblies, runtime limits, and script safety.

🎯 Steps to Execute PowerShell from C++ via C++/CLI DLL (Visual Studio 2022 / C++)

✅ Overall Structure

🔹 ① CLI Wrapper DLL (C++/CLI)

Provides functionality to execute PowerShell scripts.

🔹 ② C++ caller with /clr enabled

Calls the CLI DLL to execute PowerShell scripts.


🔧 Step 1: Create CLI Wrapper DLL Project

1. Create Project

  • [Create a new project] → [C++] → CLR Class Library (.NET Framework)
  • Name: PowerShellWrapper

💡 If this template is not displayed, install the “.NET desktop development” workload.


2. Add NuGet Package

  1. Right-click the project → [Manage NuGet Packages]
  2. Add the Microsoft.PowerShell.5.ReferenceAssemblies NuGet package, version 1.1.0. It provides reference assemblies for Windows PowerShell 5.1.

3. Implement CLI Side Code

PowerShellWrapper.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#pragma once

using namespace System;

namespace PowerShellWrapper {
    public ref class PowerShellExecutor {
    public:
        String^ Execute(String^ script);
    };
}

PowerShellWrapper.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
String^ PowerShellWrapper::PowerShellExecutor::Execute(String^ script)
{
    PowerShell^ ps = PowerShell::Create();
    try
    {
        ps->AddScript(script);
        Collection<PSObject^>^ results = ps->Invoke();
        if (ps->HadErrors)
        {
            String^ message = "PowerShell reported an error.";
            if (ps->Streams->Error->Count > 0)
                message = ps->Streams->Error[0]->ToString();
            throw gcnew InvalidOperationException(message);
        }

        System::Text::StringBuilder^ sb = gcnew System::Text::StringBuilder();
        for each (PSObject^ result in results)
            sb->AppendLine(result->ToString());

        return sb->ToString();
    }
    finally
    {
        delete ps;
    }
}

🔧 Step 2: Create the caller with /clr enabled

1. Create C++ Console App Project

  • Name: NativeApp

2. Add Reference

  • Right-click the project → [Add] → [Reference] → [Projects] → Add PowerShellWrapper
  • [Configuration Properties] → [C/C++] → [Common Language Runtime Support] → Change to /clr

3. Execution Code (main.cpp)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#using <System.dll>

using namespace System;
using namespace PowerShellWrapper;

int main()
{
    PowerShellExecutor^ executor = gcnew PowerShellExecutor();
    String^ result = executor->Execute("Get-Process | Select-Object -First 1");
    Console::WriteLine(result);
    return 0;
}

💡 Note (IDL / TLB / COM Registration)

  • Since this configuration references the .NET DLL directly instead of using COM, IDL, TLB, and COM registration are not required.

✅ Final Structure

1
2
3
4
5
Solution
├── PowerShellWrapper (C++/CLI DLL)
│   └── PowerShellExecutor
├── NativeApp (C++ EXE, /clr)
    └── main.cpp → Call DLL

⚠️ Runtime and security notes

The target Windows machine must provide Windows PowerShell 5.1 and a compatible .NET Framework.

This example targets Windows PowerShell 5.1 and .NET Framework 4.7.2 or later. Install “C++/CLI support for v143 build tools” in Visual Studio Installer and select the matching target framework.

This example does not run PowerShell 7. For PowerShell 7, design a separate host targeting the same .NET runtime, or launch pwsh as a child process. C++/CLI projects targeting modern .NET build as DLLs.

AddScript executes its string as code. Never concatenate untrusted input. For user-supplied values, prefer fixed commands with parameters and consider a constrained runspace. Scripts run with the application’s user permissions. This sample uses the default runspace.

Build both projects for the same .NET Framework target, configuration (Debug/Release), and platform architecture (x86/x64). Enabling /clr makes the caller a mixed-mode executable, not a native-only executable.

Official documentation

comments powered by Disqus