Returning Text Information from VBScript To PowerShell

by Jun 4, 2009

Sometimes, you may want to recycle old and proven VBScript scripts and call them from within PowerShell. In a previous tip, you have learned how to do that and use numeric return values to communicate. Today, let's look at how VBScript and PowerShell can communicate even better and exchange text information.

First, let's look at this really simple VBScript as it merely asks for your name and then outputs it using WScript.Echo. Save this script as client.vbs:

antwort = InputBox("Your Name?")
WScript.Echo antwort

Next, let's look at PowerShell and how PowerShell can call this script and read back whatever the VBScript has output:

$answer = cscript.exe c:…path_to_your_VBScriptclient.vbs
"Hello $answer"

PowerShell calls the VBScript using cscript.exe, the console based script host. Whenever you run a VBScript with cscript.exe, anything output via WScript.Echo is written to the WSH console.

PowerShell by default captures any console output from any external console application so all that is required is that you catch the output in a variable. That's all.