Marking Scripts for PowerShell Core or Windows PowerShell

by May 27, 2019

As you probably know, there are two breeds of PowerShell: Windows PowerShell ships with Windows operating systems and is based on the full .NET Framework whereas PowerShell 6 and better is open-source, cross-platform, and based on the (limited) .NET Core and Standard.

If you write scripts that run on both breeds, that’s awesome! If, however you know that your code requires either one, make sure you add the appropriate #requires statement on top of your script.

This piece of code will only run in PowerShell 6 and better (provided you save it to file first):

#requires -PSEdition Core 
"This runs in PowerShell 6 and better only..."

Likewise, this will run on Windows PowerShell only:

#requires -PSEdition Desktop 
"This runs in Windows PowerShell only..."

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!