Prof. Powershell

Scripting Requirements

Is it a PowerShell 1.0 script or is it version 2? Keep on top of them with this simple scripting tip.

Scripting in PowerShell 2.0 offers many new enhancements, which I'm sure I'll cover over the next several months. PowerShell scripts continue to use the .PS1 file extension. You should be able to run your PowerShell 1.0 scripts with little to no changes. Still, you might look at help for any cmdlets you use in your script to see if there are any new parameters that you want to leverage.

Because the extension is unchanged and you may have a mixed environment of PowerShell 1.0 and 2.0 for a while, how can you make sure that a script designed for PowerShell 2.0 isn't executed on a PowerShell 1.0 desktop? If you try to run a script that uses Invoke-WMIMethod on a PowerShell 1.0 desktop, you'll get an ugly error.


The solution is to insert a REQUIRES statement at the beginning of your script. This statement must be the first line in your script and I find it helpful to have a blank line after it. If you want to enforce a version requirement, use this:

#requires -version 2.0

If at some point there is an incremental PowerShell release, say 2.1, that your script requires, then can modify the requires line accordingly.

In addition to requiring a specific PowerShell version, you can also check if a particular PSSnapin is installed using this format:

#requires -PsSnapIn <PsSnapIn> [-Version <N>[.<n>]]

Let's say I need the PowerShell Community Extensions installed:

#requires -version 2.0
#requires -PsSnapIn PSCX

If I needed a specific version, I could have appended -Version to the line. You can have as many require statements as you like as long as they are all at the beginning of the script. If you are writing a function, insert them like this:

Function Get-Foo {
  #requires -version 2.0
  #requires -PsSnapIn PSCX

  Param ([string]$name="foo")

  #rest of function goes here...
}

If you attempt to run a function or script with requires statements, and the PowerShell session doesn't meet the requirements, the script will still fail, but you'll get a more graceful error message explaining why.

My recommendation is that you add version requirement at the beginning of all your PowerShell v2.0 scripts. Insert this line into your script template and you'll have nothing to worry about.


About the Author

Jeffery Hicks is a Microsoft MVP and an IT veteran with almost 20 years of experience, much of it spent as an IT consultant specializing in Windows server technologies. He works today as an independent author, trainer and consultant. His latest book is Managing Active Directory with Windows PowerShell 2.0: TFM (SAPIEN Press 2011). Follow Jeff on Twitter and on his blog.

Reader Comments:

Tue, Nov 10, 2009 CT USA

How would this be modified to specify v2.0 or greater (assuming that MS doesn't remove functionality in future versions)? Thanks.

Wed, Oct 28, 2009 Michael Domingo Irvine, CA

There were two comments posted to this article. Apologies are in order, as I accidentally deleted them doing some maintenance on this article. Please repost and you can also send me your gripes for my being such a dunderhead.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above