9

We are providing scripts to clients that will be working only in Powershell 2.0.

Through command we can ensure that powershell 2.0 is installed like below

$version = Get-host | Select-object Version

But if we provide script how to ensure that they are executing it from Powershell 2.0?

When executing the script , Powershell 2.0 features may give script errors while initiating the script itself.Isn't it?

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230

4 Answers4

13

You can annotate your script with #requires and state that it shouldn't run on anything less than PowerShell v2:

#requires -Version 2.0

(Side note: This is surprisingly hard to find even if you know vaguely that it exists.)

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Do you mean to say that it should be added as first line in my script in order to prevent it from working in 1.0? – Samselvaprabu Aug 02 '12 at 06:49
  • 1
    @Samselvaprabu: read this about Requirments: http://tfl09.blogspot.de/2009/01/more-in-powershells-require-statement.html – CloudyMarble Aug 02 '12 at 06:55
  • 1
    Added a link to the actual documentation. – Joey Aug 02 '12 at 07:17
  • 1
    The #Requires specifies a MINIMUM version - Not a specific version. Thus #Requires -version 2.0 will succeed in a version 3.o environment. – Thomas Lee Aug 02 '12 at 12:19
  • Indeed and that's what I stated in my answer as well. Most of the time you *do* want a minimum version, though, because you're using stuff that older versions don't have. Read their question and they just want this so that their scripts refuse to run on PS1 because they're using PS2 features. – Joey Aug 02 '12 at 12:27
2

Relying on the host version is not the safest thing to do as the script can be run on hosts that do not have the same host version as PowerShell's version.

The $PSVersionTable variable was added in v2. You can check if the variable exists, if it does you're running v2 and above, you can also check PSVersion.Major property:

if($PSVersionTable)
{
    'PowerShell {0}' -f $PSVersionTable.PSVersion.Major
}
else
{
   'PowerShell 1'
}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
2

I'd do something like this:

# get version
$ver = $PsVersionTable.psversion.major
 # Check to see if it's V2

If ($ver -NE 2) 
   {"incorrect version - exiting; return}
# Everything that follows is V2

Hope this helps!

Thomas Lee
  • 1,158
  • 6
  • 13
0

Try to use $PSVersionTable

You will get something like:

    Name                           Value
    ----                           ----- 
CLRVersion                     2.0.50727.5456 
BuildVersion                   6.1.7601.17514 
PSVersion                      2.0 
WSManStackVersion              2.0 
PSCompatibleVersions           {1.0, 2.0} 
SerializationVersion           1.1.0.1 
PSRemotingProtocolVersion      2.1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • Note that `$PSVersionTable` is absent in v1. It's a good indicator, but a naïve check just for the `PSVersion` property will not do alone. – Joey Aug 02 '12 at 06:02
  • @Joey: your right, i found this thread guess its what the OP needs: http://stackoverflow.com/questions/1825585/how-to-determine-what-version-of-powershell-is-installed – CloudyMarble Aug 02 '12 at 06:52