How to automate either PowerShell or PowerShell Core for same machine

Question

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?

Solution

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 applications, based on .NET Core (cross-platform).

  • 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.

  • System.Management.Automation - not recommended for direct use.


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