Call PowerShell Commands from VBScript

In this post, we demonstrate how to call PowerShell commands from VBScript. The method discussed here allows us to pass the output of a PowerShell command back into a VBScript variable.

Why would one ever want to call PowerShell commands from VBScript? A variety of possible reasons we are not concerned with. Let us get into the how.

The key to calling PowerShell commands from VBScript is to use a shell object along with the “StdOut.ReadAll” method. Using the shell object we execute the PowerShell command. We then read the resultant output using the “StdOut.ReadAll”¬†method.

Our case study is simple. Find out how many enabled Active Directory user accounts we have in our domain. We want this task integrated into some dino VBScript. It is arguably easier to get a count of enabled AD users using PowerShell than it is using VBScript. This is especially true if you have the ActiveDirectory module readily available. Don’t lose focus. This is just an example.

Here is a simple PowerShell command that gets a count of all enabled AD user accounts.

(get-aduser -filter * | where {$_.enabled -eq 'True'}).count

To integrate this PowerShell command into VBScript, we first need to assign the command string to a VBScript variable. We’ll call it strPSCommand.

strPSCommand = "(get-aduser -filter * | where {$_.enabled -eq 'True'}).count"

We then concatenate “powershell -command”¬†followed by strPSCommand from above into another VBScript variable. We’ll call this one strDOSCommand.

strDOSCommand = "powershell -command " & strPSCommand & ""

Next, we execute the combined command in VBSCript using the Shell object’s Exec method.

Set objExec = objShell.Exec(strDOSCommand)

And, finally, we read the PowerShell output using “StdOut.ReadAll” and assign the result to a VBScript variable.

strPSResults = objExec.StdOut.ReadAll

When we put it all together, here is how it looks in VBScript:

' CallPowerShell.vbs
' Call PowerShell from VBScript
' Author: ITomation (http://itomation.ca)
' Version 1.0 - 2015-11-27
' --------------------------------------------'

Option Explicit
Dim strPSCommand
Dim strDOSCommand
Dim objShell
Dim objExec
Dim strPSResults

' Construct PowerShell Command (PS syntax)
strPSCommand = "(get-aduser -filter * | where {$_.enabled -eq 'True'}).count"

' Consruct DOS command to pass PowerShell command (DOS syntax)
strDOSCommand = "powershell -command " & strPSCommand & ""

' Create shell object
Set objShell = CreateObject("Wscript.Shell")

' Execute the combined command
Set objExec = objShell.Exec(strDOSCommand)

' Read output into VBS variable
strPSResults = objExec.StdOut.ReadAll

' Echo results
WScript.Echo(strPSResults)

To run the above VBScript, open a command prompt and execute cscript CallPowerShell.vbs. If the ActiveDirectory module is available on the machine, it should simply import it upon execution.

ps1

And, last but not least, the output. A count of active users in a domain, obtained via a PowerShell command, executed from within VBScript.

ps2

We can also call entire PowerShell scripts from within VBScript. See Call PowerShell Scripts from VBScript to find out how.

1 thought on “Call PowerShell Commands from VBScript”

  1. Hans-Joachim Bolle

    Thanks, I tried many examples and finally I have one that works with giving back the data to VBS

Leave a Comment

Your email address will not be published. Required fields are marked *