从 5.1 版本开始,PowerShell 在具有不同功能集和平台兼容性的不同版本中可用。
桌面版: 基于 .NET Framework 构建,并与脚本和模块兼容,这些脚本和模块面向在 Windows 的完全占用版本(如 Server Core 和 Windows Desktop)上运行的 PowerShell 版本。
Core Edition: 基于 .NET Core 构建,并提供与脚本和模块的兼容性,这些脚本和模块面向在缩减的 Windows 版本(如 Nano Server 和 Windows IoT)上运行的 PowerShell 版本。
PowerShell 的运行版本显示在 $PSVersionTable 的 PSEdition 属性中。
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.14300.1000
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
CLRVersion 4.0.30319.42000
BuildVersion 10.0.14300.1000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
脚本作者可以阻止脚本执行,除非该脚本在兼容版本的 PowerShell 上使用语句上的 #requires PSEdition 参数运行。
Set-Content C:\script.ps1 -Value "#requires -PSEdition Core
Get-Process -Name PowerShell"
Get-Content C:\script.ps1
#requires -PSEdition Core
Get-Process -Name PowerShell
C:\script.ps1
C:\script.ps1 : The script 'script.ps1' cannot be run because it contained a "#requires" statement for PowerShell editions 'Core'. The edition of PowerShell that is required by the script does not match the currently running PowerShell Desktop edition.
At line:1 char:1
+ C:\script.ps1
+ ~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (script.ps1:String) [], RuntimeException
+ FullyQualifiedErrorId : ScriptRequiresUnmatchedPSEdition
PowerShell 库用户可以找到特定 PowerShell 版本上支持的脚本列表。 没有PSEdition_Desktop和PSEdition_Core标记的脚本被视为在 PowerShell 桌面版本上正常工作。
# Find scripts supported on PowerShell Desktop edition
Find-Script -Tag PSEdition_Desktop
# Find scripts supported on PowerShell Core edition
Find-Script -Tag PSEdition_Core