Nowadays, often your PowerShell code uses features unavailable in older PowerShell versions. When running it on a version that is too old, you usually get an error message, for instance like this:
Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.
Back in the days, this was a new feature introduced in PowerShell 3.0: [WayBack] Use cases of [ordered], the new PowerShell 3.0 feature – Stack Overflow
It is way friendlier to show a message indicating the version is too old in stead of throwing this error.
That’s where the # Requires Version 3.0
directive comes in: [WayBack] about_Requires | Microsoft Docs.
Adding this line to the top of a script gives output like this on a stock Windows 7 SP1 system that has PowerShell 2.0:
# PowerShell -f List-Delphi-Installed-Packages.ps1
The script ‘List-Delphi-Installed-Packages.ps1’ cannot be run because it contained a “#requires” statement at line 1 for Windows PowerShell version 3.0. The version required by the script does not match the currently running version of Windows PowerShell version 2.0.
+ CategoryInfo : ResourceUnavailable: (List-Delphi-Installed-Packages.ps1:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion
Note that PowerShell 3.0 is also the minimum version for debugging it in Visual Studio Code (which means you do not have to use PowerShell ISE any more; it is still there , but so far behind as a development tool that many prefer Visual Studio Code):
- [WayBack] PowerShell editing with Visual Studio Code
- [WayBack] Using Visual Studio Code for PowerShell Development | Microsoft Docs
- [WayBack] Introducing the Windows PowerShell ISE | Microsoft Docs
–jeroen