Call PowerShell Scripts from VBScript

Posted by ITomation

In another post, we demonstrated how to call PowerShell commands from VBScript. That method makes sense for simple one-line PowerShell commands. But what if we want to call long blocks of PowerShell code from within VBScript? Like an entire PowerShell script. Worry not…it’s just as simple, if not simpler!


Instead of calling the PowerShell code using the -command switch, we can call an entire PowerShell script by using the -file switch. For example, to call an entire PowerShell script named test.ps1 located in the same directory as our VBScript, we construct the DOS command as follows.


strDOSCommand = "powershell -file test.ps1"


This DOS command tells PowerShell to execute a file named test.ps1 all at once. The test.ps1 file will encapsulate all of our PowerShell code. This means that we actually end up having no PowerShell code within our VBScript other than the DOS command. As we did with running PowerShell commands using the -command switch, we will execute our DOS command using a VBScript Shell object.


Set objExec = objShell.Exec(strDOSCommand)


The last missing piece to call PowerShell scripts from VBScript is to make sure we add a write-host line to any results we want passed into VBScript from our PowerShell script. After all, we are getting our results back to VBScript based on the PowerShell’s output using the StdOut.ReadAll method. In other words, if the result isn’t written as output in our PowerShell script, our VBScript will not see it. Here is one example of outputting the results in PowerShell.




At last, we end up with two scripts in total. One VBScript to call the PowerShell script via DOS command, and the PowerShell script itself. Here is an example of two working scripts.


The VBScript.


' CallPowerShell.vbs
' Call PowerShell from VBScript
' Author: ITomation (
' --------------------------------------------'

Option Explicit
Dim strDOSCommand
Dim objShell
Dim objExec
Dim strPSResults

' Consruct DOS command to call PowerShell script (DOS syntax)
strDOSCommand = "powershell -file test.ps1"

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

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

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

' Echo results


And the PowerShell script we are calling.


' test.ps1
' Author: ITomation (
' --------------------------------------------'

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

Leave a Reply

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