Export (0) Print
Expand All

WshNamed Object

Updated: September 2010

Provides access to the named arguments from the command line.

Wsh Named Object graphic

The Named property of the WshArguments object returns the WshNamed object, which is a collection of arguments that have names. This collection uses the argument name as the index to retrieve individual argument values. There are three ways to access sets of command-line arguments.

  • You can access all of the arguments (those with and without names) with the WshArguments object.

  • You can access the arguments that have names with the WshNamed object.

  • You can access the arguments that have no names with the WshUnnamed object.

The following JScript example displays the following:

  • All command-line arguments that are in the WshArguments object.

  • Unnamed command-line arguments that are in the WshUnnamed object.

  • A named command-line argument that is in the WshNamed object.

// Show all of the arguments.
WScript.Echo(WScript.Arguments.length + " arguments")

for (var i = 0; i <= WScript.Arguments.length - 1; i++) {
    WScript.Echo(" " + WScript.Arguments.Item(i));

}

// Show the unnamed arguments.
WScript.Echo(WScript.Arguments.Unnamed.length + " unnamed arguments")

for (var i = 0; i <= WScript.Arguments.Unnamed.length - 1; i++) {
    WScript.Echo(" " + WScript.Arguments.Unnamed.Item(i));
}

// Show the named arguments.
WScript.Echo (WScript.Arguments.Named.length + " named arguments")
WScript.Echo (" ab: " + WScript.Arguments.Named.Item("ab"));

To run this example, perform the following steps.

  1. Paste the code into a file named ShowArgs.js.

  2. Type the following at a command prompt:

    cscript showargs.js /ab:cd 123 "scripts are wonderful"
    

Following is the output:

3 arguments
 /ab:cd
 123
 scripts are wonderful
2 unnamed arguments
 123
 scripts are wonderful
1 named arguments
 ab: cd

The following Visual Basic Scripting Edition (VBScript) example displays the following:

  • All command-line arguments that are in the WshArguments object.

  • Unnamed command-line arguments that are in the WshUnnamed object.

  • A named command-line argument that is in the WshNamed object.

Dim i

' Show all of the arguments.
WScript.Echo WScript.Arguments.Count & " arguments"

For i = 0 to WScript.Arguments.Count - 1
    WScript.Echo " " & WScript.Arguments.Item(i)
Next


' Show the unnamed arguments.
WScript.Echo WScript.Arguments.Unnamed.Count & " unnamed arguments"

For i = 0 to WScript.Arguments.Unnamed.Count - 1
    WScript.Echo " " & WScript.Arguments.Unnamed.Item(i)
Next


' Show the named arguments.
WScript.Echo WScript.Arguments.Named.Count & " named arguments"
WScript.Echo " ab: " & WScript.Arguments.Named.Item("ab")

To run this example, perform the following steps.

  1. Paste the code into a file named ShowArgs.vbs.

  2. Type the following at a command prompt:

    cscript showargs.vbs /ab:cd 123 "scripts are wonderful"
    

Following is the output:

3 arguments
 /ab:cd
 123
 scripts are wonderful
2 unnamed arguments
 123
 scripts are wonderful
1 named arguments
 ab: cd

Date

History

Reason

September 2010

Enhanced the examples.

Customer feedback.

Community Additions

Show:
© 2014 Microsoft