5

I have a VBscript file. I run this VBscript using CScript on windows 2012 server. The VBscript runs fine on the server.

But I need to call this VBScript file from Powershell. This is what I did.

For simplicity, this is what I have in my VBscript file

echo.vbs

Msgbox("hello world")

I wrote the test.ps1

    $acommand = "C:\Windows\System32\Cscript.exe C:\deleteit\echo.vbs"

    Invoke-Expression $acommand
5
  • 1
    ... and what happened? I suggest that if possible you convert your VBS to PS and avoid this. You are basically call an old style scripting language from a modern scripting language. It'll be simpler if you stick to one platform Commented Oct 6, 2017 at 3:45
  • 1
    Why are you doing that at all? The right way to run an exe is to just run an exe. PowerShell is a shell, it can run things directly. PS C:\> c:\windows\system32\cscript.exe c:\deleteit\echo.vbs Commented Oct 6, 2017 at 3:55
  • PS C:\> C:\>cd c:\windows\system32\cscript.exe c:\deleteit\echo.vbs C:\>cd : The term 'C:\>cd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + C:\>cd c:\windows\system32\cscript.exe c:\deleteit\echo.vbs + ~~~~~~ + CategoryInfo : ObjectNotFound: (C:\>cd:String) [], CommandNotFo undException + FullyQualifiedErrorId : CommandNotFoundException Commented Oct 6, 2017 at 16:40
  • @Jason why are you typing the prompt text and why are you trying to change directory into cscript.exe? The bit you type is c:\windows\system32\cscript.exe c:\deleteit\echo.vbs Commented Oct 6, 2017 at 17:18
  • While your call technically works, Invoke-Expression (iex) should generally be avoided, because it can be a security risk and because better solutions are usually available. Direct invocation, as shown in js2010's answer, is the right approach; see this answer for additional information. Commented Aug 13, 2024 at 18:20

2 Answers 2

9

Aside from simply running

Cscript.exe C:\deleteit\echo.vbs //nologo

There's a com object that can embed vbscript right into powershell, but it's 32-bit only. There's a way to run jobs as 32 bit. In powershell 7 you have to use the 32-bit version.

start-job {
  $sc = New-Object -ComObject MSScriptControl.ScriptControl.1
  $sc.Language = 'VBScript'
  $sc.AddCode('
    Function MyFunction(byval x)
      MyFunction = 2 * x
    End Function
  ')
  
  $sc.codeobject.MyFunction(1)
  $sc.codeobject.MyFunction(2)
} -runas32 | wait-job | receive-job

Output:

2
4
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for showing direct-invocation syntax, but the COM solution, especially given its 32-bit constraint, is of little practical use. Note that -RunAs32 seems to be ignored in Windows PowerShell v5.1.22621.3880, so the solution as shown no longer works. In PowerShell 7, you'd need to install a 32-bit version (too).
1

It is the right way to run an external application and you can use the same technique if you are using command line exe's or VBS scripts.

Personally, I would be looking to add the functionality to a PowerShell script rather then calling an external VBS script, but that's just my 2 cents worth :)

4 Comments

"It is the right way to run an external application" - it so isn't. Directly running the application is most right, the & call operator is next most right if it has to be in a string to handle spaces in the path, and Start-Process is next most right if you need to play with arguments and window styles and waiting. Invoke-Expression is least right - it's wrapping a powershell command into a string and feeding it back into the powershell engine. It's circular and redundant - whatever was in the string would work if taken out of the string and not fed into invoke-expression.
@TessellatingHeckler The issue is using the VBScript like this in the first place instead of just using PowerShell. I don't think what Gareth says is wrong if anything you both agree, the down-vote is harsh imho.
I have a reason why I am not using powershell. the VB script that I wrote is using a DLL . even though I registered on the box, powershell says it can't find it. But VBScript can see the DLL. So I had to use VBscript
@Lankymart downvote is harsh? How else should I signal disagreement? It's the only mechanism the site allows. If someone googles this and finds an answer saying ("I would do it differently and rewrite it") that's unhelpful opinion and not an answer, and if they find ("Invoke-Expression is the right way to run programs") that's plainly wrong and a bad habit to get into due to the potential for injection style attacks of running arbitrary PowerShell code from a string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.