BuddyJoe
BuddyJoe

Reputation: 71101

PowerShell - how to properly embed in Windows Forms project - .NET 4.7.2

I am using a Windows Forms project just to try to test out some ideas in scripting inside an application with PowerShell scripts. I having trouble figuring out what Nuget package to add.

I have tried multiple versions of System.Management.Automation to my project. https://foxlearn.com/windows-forms/how-to-execute-powershell-script-in-csharp-444.html the article above doesn't really get into version information. When I add the Nuget package I don't see Runspace or PowerShell objects.

I am using Winforms under C# / .NET version 4.7.2. Also, which library/package should you use under a .NET 5 console app?

Upvotes: 0

Views: 932

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

I am using Winforms under C# / .NET version 4.7.2

Assuming you want the latest available version for that runtime, you'll want to reference the "PowerShell 5.1 Reference Assemblies" package:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.PowerShell.5.1.ReferenceAssemblies" version="1.0.0" targetFramework="net472" />
</packages>

As the name implies, this package is only a shallow metadata reference, and the resulting binaries will have a runtime dependency on Windows PowerShell 5.1 (default version in all currently supported versions of Windows 10).


Also, which library/package should you use under a .NET 5 console app?

In this case, you will want to pick the latest compatible version of the System.Management.Automation.


If you find yourself in a situation where you have to develop shared PowerShell components for both .NET Framework >4.5 and .NET 5, you might want to take advantage of the PowerShellStandard.Library package - it's a .NET Standard package that exposes only the parts of the API that's available in both 5.1 (so it works with .NET Framework apps) and 6 (so it also works in .NET Core / .NET 5)

Upvotes: 2

Related Questions