How Can I Save Output to a Text File?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there an easy way to save my script output to a text file rather than displaying it on screen?

— KP, Ogden, UT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KP. If you always want your script to save data to a text file rather than display it on screen, then you’re probably better off using the FileSystemObject and its file-writing capabilities. However, it appears as though you sometimes want to save data to a text file and sometimes want to display that data in a command window. So can you create a multi-purpose script like that?

Well, you could, but an easier way is to use the command shell redirection characters when you start the script. For example, you might typically start your script like this:

cscript myscript.vbs

That runs the script, and causes any Wscript.Echo commands to display their output in the command window.

However, you could also start your script like this, using the > command shell redirection command:

cscript myscript.vbs > c:\scripts\log.txt

In that case, nothing will be echoed to the screen; instead, all your Wscript.Echo commands will be written to the text file C:\Scripts\Log.txt.

In the preceding example, should C:\Scripts\Log.txt already exist it will be overwritten with any new information generated by the script. If you’d prefer to have new information appended to C:\Scripts\Log.txt, then start the script using this command:

cscript myscript.vbs >> c:\scripts\log.txt

Comments (10)

  1. netra says:

    can v save the data on the o/p screen as it is? along with the data tht is present in our program?

    in the sense: if im writing a prog to say

    the current value is : ____ and the user enters the values there.. is it possible to save all of this as a text file?

  2. Anderson Ferneda says:

    Hi all.

    This option doesn't work when used on scheluded task.

    I need to put any command in the script to do this.


  3. kenny says:

    Strange, I ran this in a .bat file with a script that I run to see if PCs are responding on the network.

    When I run the script manually, all 3 PCs come back as responding to pings.

    When I run the .bat file, the txt file indicates that the PCs are not responding.

    Why would this command return the opposite result of what is expected?

  4. Rick says:

    How can I output only the failed pings to the log.txt file?

  5. Rick says:

    okay, how can I change the color of the failed pings on that outputted >>log.txt file?

  6. scott carpenter says:

    txt files don't have color attributes, try RTF but I don't know if a batch file will do that (or even VBScript, maybe it will)

  7. scott carpenter says:

    I did a similar ping -> log  file thing but what I did was display the results in a auto refresh html file and color coded the DIV behind the text based on the ping, I did it in powershell but VBScript will definitely work

  8. colin says:


    I would appreciate if you could share a power-shell script through which I can simply multiple perform telnet from an internal server and generate a report or a log file which informs me which telnet commands were successful and which failed.

    For example:

    telnet 443

    telnet 22

    telnet 21

    and then a report

    443 passed

    22 failed.


  9. Lens says:

    I put this at the top of myscript.vbs and got a compilation error

  10. L3n4rD says:

    not work for may telnet., it only save the title before the telnet., what i need is to save all logs in telnet