次の方法で共有


互換性のある PowerShell エディションを使用したスクリプト

PowerShell は、バージョン 5.1 以降、機能セットとプラットフォーム互換性が異なるさまざまなエディションが提供されるようになりました。

  • 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

スクリプト作成者は、 #requires ステートメントの PSEdition パラメーターを使用して互換性のあるエディションの PowerShell で実行されない限り、スクリプトの実行を防止できます。

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 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

詳細