Run exe file created from VB Script with no interaction


I have created an exe file (converted from vbscript to protect any changes in code, by mistake or otherwise). This script (vbscript) can be used with "cscript from command prompt and pipe the results in a text file in the same location from where it ran. After changing to exe, now this starts with WSH by default and when tries to run using cscript parameter gives error:
"Input Error: There is no script engine for file extension ".exe".
I am tried using batch script to call exe and pipe result in txt, did not help.
Here is what I need, any help is much appreciated.
VBScript gives output in command prompt when run in cscript and result can be piped into a text file.
Vbscript converted to exe
Now run this exe (by any script or method), should not be interactive, quietly runs or show progress on screen and display message when complete and result is saved in text file (result from vbscript/exe)

Thanks for help
Who is Participating?
mohi09Connect With a Mentor Author Commented:
That is good but it is not piping the result to text file. Now what did is added the code to write in text file to avoid the piping stuff. With this I don't need to switch the script host. I want to create and write the text file to the same location from where the exe is execute. if it is on desktop then the text file should be on the desktop. Right now I am doing to the fixed path in root which is not desirable depending on the machine this script need to run

strFile = "UserList.txt"
strWritePath = "C:\" & strFile
strDirectory = "C:\"
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
If objFSO1.FileExists(strWritePath) Then
Set objFolder = objFSO1.GetFile(strWritePath)
    Set objFile = objFSO1.CreateTextFile(strDirectory & strFile)
      objFile = ""
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set textFile = fso.OpenTextFile(strWritePath, OPEN_FILE_FOR_WRITING)
textFile.WriteLine("User : " & objUser.sAMAccountName ") 

Open in new window

Michael PfisterCommented:
What did you use to convert the VBS to an EXE? If it doesn't h ave the option to use cscript instead of wscript, you may need to write to the output file in your script.
maybe running a

cscript //H:CScript
helps, but this is just a guess.

If you need to write to a text file from VBScript, see
mohi09Author Commented:

I used vbsEdit tool which have option to convert vbscript to an exe.
When I exceute this it starts, could not force to start with cscript. With cscript I can give parameter to pipe the result in text file.
If there is any code I add vbscript itself before converting to exe which can do this two steps:
Write to a text file from same location where it run and
Run quietly and notify when complete.
RobSampsonConnect With a Mentor Commented:
Hi, you can try adding this block of code to the top of your code before compiling it:

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
   Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
End If

' Rest of normal code goes here


Then, when you run it just by double-clicking the EXE, it will detect which engine it's using, and force it to use CScript. I'm not sure though, that the pipe will work just yet, we may need to build that into the block above as well...


RobSampsonConnect With a Mentor Commented:
Hi, I don't think you need to test if the file exists first, seeing as you're opening it for writing anyway, it will just overwrite the file, so instead, we can just use CreateTextFile, forcing it to overwrite in the first place.

Anyway, I've added strCurrentDir, which points to the folder that the script is running from.


strCurrentDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strFile = "UserList.txt"
strWritePath = strCurrentDir & "\" & strFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set textFile = objFSO.CreateTextFile(strWritePath, OPEN_FILE_FOR_WRITING, True)
textFile.WriteLine("User : " & objUser.sAMAccountName)

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.