How to automate either PowerShell or PowerShell Core for same machine
With the release of PowerShell Core, how can an application choose which version of Powershell (Powershell 5.x or PowerShell Core) is being called when it using hosting automation library (system.management.automation)? Something about the runspace that should be created? or maybe the connection info?
Here's an overview of the PowerShell SDK-related NuGet packages:Adapted from here.
-
Microsoft.PowerShell.5.ReferenceAssemblies
- for building Windows PowerShell applications, based on the .NET Framework (Windows-only). -
Microsoft.PowerShell.SDK
- for building PowerShell (Core) v6+ applications, based on .NET Core / 5+ (cross-platform).- To determine what specific .NET (Core) runtime a given package version must be combined with at a minimum, select the package version of interest on the linked page, expand the
Dependencies
section, and consult the first entry; e.g., for package version7.2.0
the minimum required .NET (Core) runtime isnet6.0
.
- To determine what specific .NET (Core) runtime a given package version must be combined with at a minimum, select the package version of interest on the linked page, expand the
-
PowerShellStandard.Library
- for building modules and hosts that are compatible with both Windows PowerShell and PowerShell Core (cross-platform) - but note:-
"PowerShell Standard is a reference assembly that has been created to assist developers in creating modules and PowerShell hosts which will run on PowerShell. The reference assembly contains no actual implementation but rather will allow you to use only APIs that exist across different versions of PowerShell. This means that you still need to run within a PowerShell runtime."
-
For creating stand-alone applications, use one of the first two packages.
-
-
- not recommended for direct use.System.Management.Automation
As for targeting a specific edition / version via remoting:
-
See this question and answer
-
Note that it covers remoting from the perspective of using PowerShell cmdlets, not the SDK, though you can always call the cmdlets via the SDK as well.
As Lee Daily points out in a comment, the edition-specific executable filenames are:
-
powershell.exe
- Windows PowerShell -
pwsh.exe
(Windows) /pwsh
(Unix-like platforms) - PowerShell (Core) v6+