Converting the Windows Script Host Exec Method

Definition: Runs an application in a child command-shell, providing access to the StdIn/StdOut/StdErr streams.

Exec

In Windows Script Host the Exec method allows you to run a command-line utility and then store the output of that utility in a variable; this enables you to capture and analyze the output as opposed to merely displaying it on screen. In Windows PowerShell, you can accomplish this same feat simply by calling the command-line utility and assigning the returned information to a variable. For example, this command calls the executable file IPConfig.exe, and stores the resulting information in a variable named $a:

$a = ipconfig

If you now echo back the value of $a you should see something similar to this:

Windows IP Configuration

Ethernet adapter Local Area Connection:

        Media State . . . . . . . . . . . : Media disconnected

Ethernet adapter Wireless Network Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.1.100
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.1.1

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