Hidden PS window via VBS with return code


I am trying to use a VBScript helper script to launch a PowerShell script in a hidden window. I have been doing this successfully for some time, but now I want to add getting a return code, and that makes things more complicated.
The usual approach for getting a return code is to use .Exec rather than .Run, but then you lose the ability to hide the command window. I found this thread, and based on that and a known working strCMD, I tried

But that doesn’t work. Any suggestions where I am going wrong? Or suggestions on a better way to get a hidden Powershell script with return code? FWIW, I have to target PS 2.0. 🙁

For completeness, the value of strCMD is

And TestMessage.ps1 is simply

Thanks to omegastripes’ link, I am trying to implement a more general and PowerShell focused version of this implementation, with some code refactor to eliminate the one time calls to functions and awkward forced exits. What I have is this:

Which in turn calls this PS1

This works in that it does run the PS1, and it is hidden, but I am having trouble getting results back. I have tried Return, Write-Output and Write-Host and none sends the results from the PS1 back to the VBS. I am also having some issues when passing arguments, and I really don’t like the For Each loop with a forced Exit internally, but that is polish. The bigger issue is how to consistently get results back from the PS1.


Your powershell script needs to return an exit code through

Your WScript.Shell’s Run method in VBS will then return this code so you need to save it.

So Result now contains the Exit code so you can now either use it or return it again and exit the process.


Hidden PS window via VBS with return code by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply