Search PowerShellers and other PowerShell-related sites

Tuesday, June 10, 2008

The "#requires" statement

Most of us are familiar with #requires -version 2. Jeffrey Snover had written about versioning just before the release of a Community Technology Preview (CTP) of Windows PowerShell v2.0. You start your script with
#requires -version 2
and PowerShell will check version #'s and produce a precise error message.

The "#requires" statement is not a new feature of PowerShell v2. It's with us from the PowerShell v1, but no one cared outside the PowerShell team. ;-)

Do you know that you can check for the presence of the other things too?

The "#requires" statement must be in one of the following formats:
"#requires -shellid <shellid>"
"#requires -version <major.minor>"
"#requires -pssnapin <pssnapinname>[-version <major.minor>]"

For example, you can check if quest.activeroles.admanagement snap-in is added to the current console. Put #requires -pssnapin quest.activeroles.admanagement in your script, and if the snap-in isn't added, you will get nice error message.

The script 'test.ps1' cannot be run because the following Windows PowerShell snap-ins that are specified
by its "#requires" statements are missing: quest.activeroles.admanagement.

By the way, is there any other reserved "comment" statement?