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 an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.