Skrip dengan edisi PowerShell yang kompatibel

Dimulai dengan versi 5.1, PowerShell tersedia dalam edisi berbeda yang menunjukkan berbagai set fitur dan kompatibilitas platform.

  • Edisi Desktop: Dibangun di .NET Framework dan menyediakan kompatibilitas dengan skrip dan modul yang menargetkan versi PowerShell yang berjalan pada edisi jejak penuh Windows seperti Server Core dan Windows Desktop.

  • Edisi Inti: Dibangun di .NET Core dan menyediakan kompatibilitas dengan skrip dan modul yang menargetkan versi PowerShell yang berjalan pada edisi jejak Windows yang dikurangi seperti Nano Server dan Windows IoT.

Edisi PowerShell yang sedang berjalan diperlihatkan di properti PSEdition $PSVersionTable.

$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

Penulis skrip dapat mencegah skrip dijalankan kecuali dijalankan pada edisi PowerShell yang kompatibel menggunakan parameter PSEdition pada #requires pernyataan.

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

Pengguna Galeri PowerShell dapat menemukan daftar skrip yang didukung pada Edisi PowerShell tertentu. Skrip tanpa tag PSEdition_Desktop dan PSEdition_Core dianggap berfungsi dengan baik pada edisi PowerShell Desktop.

# Find scripts supported on PowerShell Desktop edition
Find-Script -Tag PSEdition_Desktop

# Find scripts supported on PowerShell Core edition
Find-Script -Tag PSEdition_Core

Detail Selengkapnya