Running PowerShell in a .NET Framework Application

I’m trying to run PowerShell code (as a string, i.e. “Get-Process”) in my C# .NET Framework 4.6.1 Windows console application and get the output from whatever command was run.

From what I could gather from searching, this appears complicated by the introduction of PowerShell Core, which runs on the .NET Core runtime.

It appears I can easily run this using the PowerShell class in the System.Management.Automation namespace (https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.powershell?view=powershellsdk-1.1.0). However, this is not part of the .NET Framework, it’s part of the PowerShell SDK (still not really sure what that is exactly). It’s also different than System.Management (it does not include System.Management.Automation), so I can’t include it as a reference easily in Visual Studio with the included Framework assemblies.

It is available by Microsoft as two different packages on NuGet (https://www.nuget.org/packages/System.Management.Automation and https://www.nuget.org/packages/system.management.automation.dll).

The first package (which seems like the “main” one) seems to have a dependency of the .NET Core, so I can’t use it since I’m targeting the .NET Framework if I’m understanding things correctly (it wouldn’t install when I tried). The second package looks like it should work but might only support PowerShell 3.0.

Is the best way to just install the second package from NuGet and use that?

I appreciate any information.

submitted by /u/abluedinosaur
[link] [comments]

Leave a Reply