0

If using PowerShell_ISE, when attempting to use any of the #Requires commands, for example:

#Requires -Version 3.0 

They error with the text:

An error occurred while creating the pipeline. + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : RuntimeException

I've tested this on ISE and the Console with the same results, my versions are below:

Name                           Value
----                           -----
PSVersion                      5.1.14409.1012
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1012
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
mklement0
  • 382,024
  • 64
  • 607
  • 775
ScriptMonkey
  • 191
  • 1
  • 3
  • 20
  • 1
    The `about_Requires` help topic explains how the `#requires` statement works. It prevents the _entire_ script from running if the version is insufficient. This is by design. – Bill_Stewart May 22 '18 at 15:37
  • The reason I posted this (and the answer) was that I found no information about it anywhere (including help). As I've come across the issue previously, I thought others may also benefit if they found this article. – ScriptMonkey May 23 '18 at 03:36

2 Answers2

1

My final test before posting the question provided the answer:

It seems the '#Requires' commands are designed to break out of the script if it doesn't meet the requirements rather than posting an error, and testing it on a single line will result in an error.

To test, find your PowerShell version (e.g. 5.1) and save the following transposing 5.1 with your (major.minor) version:

#Requires -version 5.1
$PSVersionTable.PSVersion
Read-Host

This will work on both ISE and the Console, providing an output even if 'Run with PowerShell'.

  • Using the #Requires -(anything) line on its own will display the error.
    • In ISE 'Run selection' will work, as long as it's accompanied by another line of code.
  • Using ISE, If the #Requires parameter doesn't match yours, it'll throw a more descriptive error. If run in the console, it'll break and close.
ScriptMonkey
  • 191
  • 1
  • 3
  • 20
  • I see. I've added the `powershell-ise` tag to your question. However, at least as of v5.1.22621.963 I don't see your symptom with `#Requires -Version 3.0`. However, I do see an error with a _nonexistent_ historical version of PowerShell, such as `#Requires -Version 3.99` - unlike in regular console windows. Perhaps you've long since moved on from the ISE anyway, but let me provide current advice in the next comment: – mklement0 Dec 18 '22 at 04:23
  • As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Dec 18 '22 at 04:23
  • Correct, PowerShell_ISE has long since been retired, as perhaps should this post. As some people including myself use ISE occasionally, it may be worth keeping it, but I'd suggest anyone NOT experiencing the issues would, by definition not be reading this post. – ScriptMonkey Dec 19 '22 at 08:03
1
$versionMinimum = [Version]'5.0'

if ($versionMinimum -gt $PSVersionTable.PSVersion) { 
    throw "You need Powershell Version $versionMinimum to run this script" 
}
f6a4
  • 1,684
  • 1
  • 10
  • 13