36

I was wondering if Visual Studio can run and debug VBScript files?

Thank you for your time.

3

5 Answers 5

82

First of all VBScript is an interpreted language and does not need to be compiled.

But yes, you can debug your VBScript file inside Visual Studio. Under "Tools / External Tools" you have to register the Windows Script Host (CScript.exe or WScript.exe) once with some startup parameters (to make it available in the Tools menu):

Title:             VBScript Debug
Command:           C:\Windows\System32\cscript.exe
Arguments:         //nologo //X $(ItemPath)
Initial directory: $(ItemDir)
Check "Use Output window"
Hit the OK button

enter image description here

Then, just open the file you want to debug with Visual Studio (via File --> Open --> File...)

In order to start the debugger, just click "Tools / VBScript Debug".

enter image description here

The VS JIT debugger asks you, which debugger you want to use. Just select your *.vbs file and click Yes.

Note: This answer is still valid and works with Visual Studio 2019 & 2022.

8
  • 3
    This is awesome! You can debug step by step, view and change locals, etc... just like in visual basic debugger!
    – cyberponk
    Mar 13, 2019 at 1:13
  • 2
    Note that checking "Use Output window" won't let you take console input. So you may want to leave that unchecked. Aug 30, 2019 at 15:04
  • 2
    Un-check "Close on exit" so you can read if errors are occurring.
    – Matt
    Feb 13, 2020 at 9:54
  • 1
    If you have 32-bit Office installed on a 64-bit system, and wish to debug office related stuff, use C:\Windows\SysWow64\Cscript.exe
    – cup
    Nov 28, 2020 at 8:50
  • 1
    @NielsLucas it needs to be .vbs, not .vb, unless that was a typo.
    – Mahen
    May 12, 2021 at 7:44
9

For future readers.

This will work in a pinch.

"C:\Windows\System32\cscript.exe" /x MyFile.vbs

You will be prompted....and you can start a new instance of VS 2xxx (<< Whatever version is installed on your computer).

Not how to do it if you do it all the time..but good for every once in a while.

1
  • 1
    If I'm not mistaken, you need to pass in a double-slash: //x. Also, if you pass in //d as well, the debugger will pause at the first line of the code even if there is no error.
    – Zev Spitz
    May 10, 2018 at 7:40
2

This worked for me when I wanted to debug slmgr.vbs script:

1) To enable the just in time debugger you have to enable it in the registry:

HKEY_CURRENT_USER\Software\Microsoft\Windows Script\Settings

set JITDebug=1

2) run command like this:

wscript.exe /x /d slmgr.vbs -xpr

3) The Visual Studio Just-In-Time debugger prompts for a possible debugger. You can just choose New instance of Microsoft Visual Studio 2012, click Yes button.

1
  • +1 -> This way i get all Vanillas from debugging in VS2012 (Breakpoints, objects listing, inspecting variables, etc...) Aug 31, 2017 at 13:37
0

A debugger that i use is VBsEdit, (click here). It is free to use, and is helpful in getting to the route of a problem quickly, it also comes with a range of scripts that you can use. The one downside with the free version is that every program load will take one second longer than the last, unless you restart VBsEdit or buy the full version. Hope this helps

3
  • Yeah i already have that, what i was looking for was the ability to have all my scripts in one "project" as such and be able to run them.
    – New Bee
    Apr 24, 2014 at 1:29
  • 10
    The latest version...every "Step Over" you do while debugging puts a few second "Register Now!" message. When you say No, it gives you a N second delay. and the "N" increases by 1 every time you click it. Gaaaaaaaaaaaaaa. For future readers. Mar 18, 2016 at 18:00
  • 1
    Yes, I was very interested in this software until I saw that, 10 second maximum between "Step Over"s lol, 59$ license to avoid this annoyance May 19, 2020 at 13:57
0

If you are interested by Visual Studio only to debug a VBScript, and nothing else, I recommend to install Visual Studio Community as it's free and install these minimum individual components so that to not occupy too much space (for VSC 2019 version 16.7.7, approximately "800MB" + "2.63 GB"):

  • Compilers, build tools, and runtimes
    • C++ CMake tools for Windows
    • MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.27)
  • Debugging and testing
    • Just-In-Time debugger

(NB: there could be other components automatically added, like "JSON editor") (NB: if you select only the JIT debugger, you'd get a popup error like "Unable to open script document. The debugger is not properly installed. Run setup to install or repair the debugger." and at the same time Visual Studio would display "Source not found" and "script.vbs not found")

After the installation, launch Visual Studio, make sure that the JIT debugger is activated for "Script" (menu Tools > Options > Debugging > Just-In-Time).

And then run the script using "C:\Windows\System32\cscript.exe" /x "My File.vbs" in the two flavors as explained in the other answers, that will start the debugger.

If you want to see some screenshots of the installation and the debugging of the VBScript, go to this other Stack Overflow question/answer: https://stackoverflow.com/questions/59111755/how-to-debug-vbscript-with-visual-studio-community-2019

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.