Dealing with “Windows PowerShell” and “PowerShell Core”

by Jul 18, 2017

There are two PowerShell Editions now: “Windows PowerShell” shipping with Windows, and running on the full .NET Framework, and the limited “PowerShell Core” running on .NET Core which is available cross-platform and runs on Nano Server, for example.

Script authors targeting a specific PowerShell edition can now use the #requires statement to make sure their scripts run on the intended edition.

For example, to ensure that a script runs on PowerShell Core, add this on top of a script:

#requires -PSEdition Core 
Get-Process

Make sure you save the code to disk. #requires applies to scripts only.

When you now run this script on a Windows machine inside a “Windows PowerShell”, it fails:

 
 
PS> C:\Users\abc\requires  core.ps1
The script 'requires  core.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.
    + CategoryInfo          : NotSpecified: (requires  core.ps1:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId :  ScriptRequiresUnmatchedPSEdition
 
 
PS>   
 

Likewise, and maybe even more important, when you replace “Core” by “Desktop”, a script will not run on the limited “PowerShell Core” editions. That’s a wise thing to do if your script uses features specifically targeting classic Windows systems, and requiring the full .NET Framework.

Twitter This Tip! ReTweet this Tip!