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
2
  • 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. Commented May 22, 2018 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. Commented May 23, 2018 at 3:36

2 Answers 2

1
$versionMinimum = [Version]'5.0'

if ($versionMinimum -gt $PSVersionTable.PSVersion) { 
    throw "You need Powershell Version $versionMinimum to run this script" 
}
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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:
As an aside: The PowerShell ISE is no longer actively developed and there are reasons not to use it (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 with its PowerShell extension.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.