What is the command or the quickest way to output results to console using vbscript?
8 Answers
You mean:
WScript.Echo "Like this?"
If you run that under wscript.exe
(the default handler for the .vbs extension, so what you'll get if you double-click the script) you'll get a "MessageBox" dialog with your text in it. If you run that under cscript.exe
you'll get output in your console window.

- 3,492
- 21
- 31

- 14,051
- 3
- 21
- 14
-
1You can directly use on the wscript.exe the function `MsgBox("text")` or `MsgBox(object.property)` but `Wscript.Echo` is easier to write. Thanks. – m3nda Feb 24 '15 at 00:04
-
28Unintuitively for me, `WScript.Echo` *must* be used for whether you're running via `WScript` or `CScript`. That is, there *is not* a `CScript.Echo`, in case future googlers wonder. (*Very* happy the msgboxes are gone [when run with `cscript`], however; thanks.) – ruffin May 11 '15 at 14:10
-
1@GabrielStaples - Not w/ the stock `WScript.Echo`. I suppose, if you wanted to stay totally within WScript you could do something horrifyingly dodgy like Exec'ing off another process to do a "SendKeys" to the parent process to close the MessageBox. – Evan Anderson Jun 22 '17 at 03:07
-
7Actually, I just found this `popup` method. Very similar to `echo` but allows you to specify a timeout after which it will automatically close the popup box. Very convenient and easy to use: https://technet.microsoft.com/en-us/library/ee156593.aspx – Gabriel Staples Jun 22 '17 at 10:45
This was found on Dragon-IT Scripts and Code Repository.
You can do this with the following and stay away from the cscript/wscript differences and allows you to get the same console output that a batch file would have. This can help if your calling VBS from a batch file and need to make it look seamless.
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
Set stderr = fso.GetStandardStream (2)
stdout.WriteLine "This will go to standard output."
stderr.WriteLine "This will go to error output."

- 11,743
- 10
- 52
- 81

- 1,545
- 11
- 12
-
9If the script is started by double-click and thus opened with wscript, the script results in an error message: "Invalid Handle". – Bernhard Hiller Mar 22 '13 at 09:09
-
10@Bernhard: You are getting this error if you run the script using wscript.exe. Wscript is windows-oriented and has no console streams. Use cscript.exe instead: http://technet.microsoft.com/en-us/library/bb490816.aspx – Axel Kemper Aug 15 '13 at 10:47
-
25@BernhardHiller has a valid point. The thrust of this answer is that using stdout directly would avoid the CScript/WScript differences. That is incorrect. This solution still only works under CScript.exe, so there doesn't seem to be much benefit over just using `WScript.Echo`. In fact, the difference is magnified, because the script will no longer run at all under WScript. Its a valid technique that has its uses, for example if one needs to write to StdErr, but in the context of this answer, it is misleading. – Tim Long Oct 12 '13 at 20:14
-
4I just want to light up the benefit of this method over `WScript.Echo`: `cscript //b foobar.vbs` Runs `foobar.vbs` without any console output, but by Rob's method you can have output even when passing `\\b` to `cscript.exe` – S. Razi Dec 15 '14 at 14:04
You only need to force cscript instead wscript. I always use this template. The function ForceConsole() will execute your vbs into cscript, also you have nice alias to print and scan text.
Set oWSH = CreateObject("WScript.Shell")
vbsInterpreter = "cscript.exe"
Call ForceConsole()
Function printf(txt)
WScript.StdOut.WriteLine txt
End Function
Function printl(txt)
WScript.StdOut.Write txt
End Function
Function scanf()
scanf = LCase(WScript.StdIn.ReadLine)
End Function
Function wait(n)
WScript.Sleep Int(n * 1000)
End Function
Function ForceConsole()
If InStr(LCase(WScript.FullName), vbsInterpreter) = 0 Then
oWSH.Run vbsInterpreter & " //NoLogo " & Chr(34) & WScript.ScriptFullName & Chr(34)
WScript.Quit
End If
End Function
Function cls()
For i = 1 To 50
printf ""
Next
End Function
printf " _____ _ _ _____ _ _____ _ _ "
printf "| _ |_| |_ ___ ___| |_ _ _ _| | | __|___ ___|_|___| |_ "
printf "| | | '_| . | | --| | | | . | |__ | _| _| | . | _|"
printf "|__|__|_|_,_|___|_|_|_____|_____|___| |_____|___|_| |_| _|_| "
printf " |_| v1.0"
printl " Enter your name:"
MyVar = scanf
cls
printf "Your name is: " & MyVar
wait(5)

- 510
- 4
- 10
-
Are you sure that answers the [actual question](http://stackoverflow.com/questions/4388879/vbscript-output-to-console)? – dakab Aug 31 '15 at 15:42
-
Yes, only call the ForceConsole() and then use printf() to print text in the output console. Also you have other alias to clear screen, scan text and wait (sleep) – MadAntrax Aug 31 '15 at 15:48
-
3Best solution ever, thank you, but only the "ForceConsole" function matters, the "printf" and the rest are totally unnecessary since if you force to close the current script on Wscript.exe instance and then run a new cscript.exe instance of the current script, then Wscript.Echo will output to that console instance... – ElektroStudios Nov 24 '19 at 11:31
There are five ways to output text to the console:
Dim StdOut : Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
WScript.Echo "Hello"
WScript.StdOut.Write "Hello"
WScript.StdOut.WriteLine "Hello"
Stdout.WriteLine "Hello"
Stdout.Write "Hello"
WScript.Echo will output to console but only if the script is started using cscript.exe. It will output to message boxes if started using wscript.exe.
WScript.StdOut.Write and WScript.StdOut.WriteLine will always output to console.
StdOut.Write and StdOut.WriteLine will also always output to console. It requires extra object creation but it is about 10% faster than WScript.Echo.

- 537
- 3
- 13
-
1... and as said in a comment to a previous answers, this doesn't work when executing with wscript.exe: https://stackoverflow.com/questions/4388879/vbscript-output-to-console#comment26762213_12865458 – maxxyme Jun 18 '19 at 09:11
-
Also found an explanation about GetStandardStream() vs WScript.StdIn/.StdOut/.StdErr : "VBScript in a Nutshell: A Desktop Quick Reference (2nd Edition)" https://books.google.fr/books?id=NLpuZSatG3QC page 298 says it's "functionnaly equivalent". – maxxyme Jul 05 '19 at 09:05
-
When you say "this doesn't work", can you specify which of the five method does not work and what you mean by not working? – Regis Desrosiers Oct 23 '20 at 14:33
I came across this post and went back to an approach that I used some time ago which is similar to @MadAntrax's.
The main difference is that it uses a VBScript user-defined class to wrap all the logic for switching to CScript and outputting text to the console, so it makes the main script a bit cleaner.
This assumes that your objective is to stream output to the console, rather than having output go to message boxes.
The cCONSOLE class is below. To use it, include the complete class at the end of your script, and then instantiate it right at the beginning of the script. Here is an example:
Option Explicit
'// Instantiate the console object, this automatically switches to CSCript if required
Dim CONS: Set CONS = New cCONSOLE
'// Now we can use the Consol object to write to and read from the console
With CONS
'// Simply write a line
.print "CSCRIPT Console demo script"
'// Arguments are passed through correctly, if present
.Print "Arg count=" & wscript.arguments.count
'// List all the arguments on the console log
dim ix
for ix = 0 to wscript.arguments.count -1
.print "Arg(" & ix & ")=" & wscript.arguments(ix)
next
'// Prompt for some text from the user
dim sMsg : sMsg = .prompt( "Enter any text:" )
'// Write out the text in a box
.Box sMsg
'// Pause with the message "Hit enter to continue"
.Pause
End With
'= =========== End of script - the cCONSOLE class code follows here
Here is the code for the cCONSOLE class
CLASS cCONSOLE
'= =================================================================
'=
'= This class provides automatic switch to CScript and has methods
'= to write to and read from the CSCript console. It transparently
'= switches to CScript if the script has been started in WScript.
'=
'= =================================================================
Private oOUT
Private oIN
Private Sub Class_Initialize()
'= Run on creation of the cCONSOLE object, checks for cScript operation
'= Check to make sure we are running under CScript, if not restart
'= then run using CScript and terminate this instance.
dim oShell
set oShell = CreateObject("WScript.Shell")
If InStr( LCase( WScript.FullName ), "cscript.exe" ) = 0 Then
'= Not running under CSCRIPT
'= Get the arguments on the command line and build an argument list
dim ArgList, IX
ArgList = ""
For IX = 0 to wscript.arguments.count - 1
'= Add the argument to the list, enclosing it in quotes
argList = argList & " """ & wscript.arguments.item(IX) & """"
next
'= Now restart with CScript and terminate this instance
oShell.Run "cscript.exe //NoLogo """ & WScript.ScriptName & """ " & arglist
WScript.Quit
End If
'= Running under CScript so OK to continue
set oShell = Nothing
'= Save references to stdout and stdin for use with Print, Read and Prompt
set oOUT = WScript.StdOut
set oIN = WScript.StdIn
'= Print out the startup box
StartBox
BoxLine Wscript.ScriptName
BoxLine "Started at " & Now()
EndBox
End Sub
'= Utility methods for writing a box to the console with text in it
Public Sub StartBox()
Print " " & String(73, "_")
Print " |" & Space(73) & "|"
End Sub
Public Sub BoxLine(sText)
Print Left(" |" & Centre( sText, 74) , 75) & "|"
End Sub
Public Sub EndBox()
Print " |" & String(73, "_") & "|"
Print ""
End Sub
Public Sub Box(sMsg)
StartBox
BoxLine sMsg
EndBox
End Sub
'= END OF Box utility methods
'= Utility to center given text padded out to a certain width of text
'= assuming font is monospaced
Public Function Centre(sText, nWidth)
dim iLen
iLen = len(sText)
'= Check for overflow
if ilen > nwidth then Centre = sText : exit Function
'= Calculate padding either side
iLen = ( nWidth - iLen ) / 2
'= Generate text with padding
Centre = left( space(iLen) & sText & space(ilen), nWidth )
End Function
'= Method to write a line of text to the console
Public Sub Print( sText )
oOUT.WriteLine sText
End Sub
'= Method to prompt user input from the console with a message
Public Function Prompt( sText )
oOUT.Write sText
Prompt = Read()
End Function
'= Method to read input from the console with no prompting
Public Function Read()
Read = oIN.ReadLine
End Function
'= Method to provide wait for n seconds
Public Sub Wait(nSeconds)
WScript.Sleep nSeconds * 1000
End Sub
'= Method to pause for user to continue
Public Sub Pause
Prompt "Hit enter to continue..."
End Sub
END CLASS

- 1,251
- 1
- 11
- 12
-
1"_include the complete class at the end of your script_" isn't necessary when using a [WSF](https://www.vbsedit.com/html/c999e0fa-3986-41be-90a9-3f8ae3a38253.asp). See also [Etalon's answer to _How do I include a common file in VBScript (similar to C #include)?_](https://stackoverflow.com/a/12374208/1744774) – Gerold Broser Feb 26 '23 at 19:26
Create a .vbs with the following code, which will open your main .vbs:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cscript.exe ""C:\QuickTestb.vbs"""
Here is my main .vbs
Option Explicit
Dim i
for i = 1 To 5
Wscript.Echo i
Wscript.Sleep 5000
Next

- 141
- 1
- 5
-
1
-
@Wolf Perhaps OP meant _quickest to write_. OK, agreed, not even that applies. :) – Gerold Broser Feb 26 '23 at 19:18
You can run this script to echo in the command line
set wshShell = createObject("wscript.shell")
wshShell.run"cmd.exe /c echo something",1

- 1
- 1
For console output I use a snippet that quits elegantly if run in the wrong run-time. (And print is shorter to type...)
Sub print(s)
On Error Resume Next
WScript.stdout.WriteLine (s)
If err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit
End Sub
print "hello"

- 714
- 1
- 6
- 14