Converting the Windows Script Host ShowUsage Method

Definition: Makes a script self-documenting by displaying information about how it should be used.

ShowUsage

The ShowUsage method – which can only be used in Windows Script Files (.WSF files) – provides a nifty way to add command-line help to a script. Thanks to ShowUsage, a user who types the /? Switch when starting your script will be presented with the help information you included in the script (using an XML-style syntax).

Windows PowerShell has nothing analogous to the ShowUsage method. (Although the Community Technology Preview releases of Windows PowerShell 2.0 feature Script Cmdlets, which offer some of the same capabilities.) If you want to include command-line help in a script you will have to “manually” look for the help switch (e.g., a question mark) and then specify all the actions the script should take. For example, this simple implementation of command-line help checks the $args collection to see if a question mark appears anywhere in the command-line arguments used when starting the script. If the question mark does appear then a one-line instruction (To use, type the name of the script followed by the IP address.) is echoed to the screen. After that, the exit command is used to terminate the script.

Here’s what the code looks like:

if ($args -match "\?")
    {Write-Host "To use, type the name of the script followed by the IP address.";exit}

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page