powershell - 安装 PowerShell 5 后,我可以在版本 3 或 4 模式下运行 PowerShell 吗?

标签 powershell

我目前安装了 Windows Management Framework 5.1。我想测试我的脚本是否可以在 PowerShell 3 和 4 中正常工作。我知道 PowerShell.exe -Version切换但是当我尝试时:

PowerShell.exe -Version 3

或者:
PowerShell.exe -Version 4

然后运行 ​​$PSVersionTable它仍然显示 PowerShell 版本 5.1 作为正在运行的版本。

如果我运行:
PowerShell.exe -Version 2

然后$PSVersionTable确实显示控制台现在是版本 2。这是为什么?

最佳答案

Windows PowerShell 提供了一个特殊的 -Version 2.0向后兼容模式,即使您安装了更高版本,也允许您运行 PowerShell 版本 2:

对于所有其他版本的 PowerShell,情况并非如此,不幸的是,当安装 PowerShell 版本 5 时,无法强制 PowerShell 作为版本 3 或 4 运行(即使运行 PowerShell.exe -version 3 不会引发任何类型的错误/警告信息)。也不可能同时安装多个 PowerShell 版本(PowerShell Core 除外,它可以与 Windows PowerShell 同时安装)。

使用 PowerShell 版本(非 2)测试 PowerShell 脚本的唯一方法是在单独的计算机或虚拟机上安装 PowerShell。

Windows PowerShell 4.0 and Windows PowerShell 3.0 are designed to be backwards compatible with Windows PowerShell 2.0. Cmdlets, providers, snap-ins, modules, and scripts written for Windows PowerShell 2.0 run unchanged in Windows PowerShell 4.0 and Windows PowerShell 3.0. However, due to a change in the runtime activation policy in Microsoft .NET Framework 4, Windows PowerShell host programs that were written for Windows PowerShell 2.0 and compiled with Common Language Runtime (CLR) 2.0 cannot run without modification in Windows PowerShell 3.0 or Windows PowerShell 4.0, which are compiled with CLR 4.0. The Windows PowerShell 2.0 Engine is intended to be used only when an existing script or host program cannot run because it is incompatible with Windows PowerShell 4.0, Windows PowerShell 3.0, or Microsoft .NET Framework 4. Such cases are expected to be rare.

关于powershell - 安装 PowerShell 5 后,我可以在版本 3 或 4 模式下运行 PowerShell 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457274/

相关文章:

C# Runspace Powershell(交互式)

powershell - 当保存脚本的文件夹名称包含空格时从任务计划程序运行 Powershell 脚本

regex - $matches ["x"] 在 ISE 中工作,但在命令行 shell 中不起作用

powershell - 尝试从 .bat 文件启动 powershell 脚本并获取返回值

c# - 如何在 PowerShell C# cmdlet 中使用 WriteProgressCommand?

powershell - 听起来很复杂,但事实并非如此,在PowerShell中比较文件

regex - 解析文件夹中的脚本文件,并在Powershell + RegEx中标记多行字符串

linux - Powershell查询WINDOWS->LINUX

powershell - 过滤AD组

powershell - PowerShell不按顺序执行Invoke-Expression调用