How can I run PowerShell with the .NET 4 runtime?

前端 未结 11 2351
走了就别回头了
走了就别回头了 2020-11-22 07:38

I am updating a PowerShell script that manages some .NET assemblies. The script was written for assemblies built against .NET 2 (the same version of the framework that Power

11条回答
  •  遥遥无期
    2020-11-22 08:04

    If you don't want to modify the registry or app.config files, an alternate way is to create a simple .NET 4 console app that mimicks what PowerShell.exe does and hosts the PowerShell ConsoleShell.

    See Option 2 – Hosting Windows PowerShell yourself

    First, add a reference to the System.Management.Automation and Microsoft.PowerShell.ConsoleHost assemblies which can be found under %programfiles%\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

    Then use the following code:

    using System;
    using System.Management.Automation.Runspaces;
    using Microsoft.PowerShell;
    
    namespace PSHostCLRv4
    {
        class Program
        {
            static int Main(string[] args)
            {
                var config = RunspaceConfiguration.Create();
                    return ConsoleShell.Start(
                    config,
                    "Windows PowerShell - Hosted on CLR v4\nCopyright (C) 2010 Microsoft Corporation. All rights reserved.",
                    "",
                    args
                );
            }
        }
    }
    

提交回复
热议问题