從 5.1 版開始,PowerShell 提供代表各種功能集和平台相容性的不同版本。
Desktop Edition︰建置在 .NET Framework 上,並與 Server Core 和 Windows Desktop 等完整版 Windows 上執行之目標 PowerShell 版本的指令碼和模組相容。
Core Edition︰建置在 .NET Core 上,並與 Nano Server 和 Windows IoT 等縮減版 Windows 上執行之目標 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