Closing cmd window after script execution

The next script opens a cmd window to call an external command, and after using it the cmd window stays open. how do i close it??

Because wshShell.Quit doesn't work, it returns an error. Is there any of way of closing it?

this is the script

Option Explicit
Dim objWMIService, objProcess, objFSO, objTextFile
dim colProcess,colServices, colItems
dim objItem, objService
Dim strComputer, strProcessKill, strProcessMem
Dim memLimit, processMem
Dim mustKill
Dim WshShell
Const ForAppending = 8

Set WshShell = WScript.CreateObject("WScript.Shell") 
strComputer = "."
strProcessKill = "'DLLHOST.EXE'"
strProcessMem = "'DLLHOST'"
memLimit = 10000
mustKill = 0
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )
dim intRC
For Each objProcess in colProcess    
    processMem = objProcess.WorkingSetSize/1024
                If processMem >= memLimit Then
		       mustKill = 1
                End If    
    if mustKill = 1 then		
 	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile ("c:\informe_dllhost.txt", ForAppending, True)
	objTextFile.WriteLine(now & vbnewline & " Process has been killed with " & processMem & " Kb" & vbnewline & vbNewLine)
	objTextFile.Close
	wshShell.Run "cmd /k C:\pskill.exe -accepteula dllhost.exe", 1, True	
	WScript.Quit
    end if
Next
 
WScript.Quit

Open in new window

LVL 1
celticianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Surone1Commented:
the easiest way is creating a 2 line bat file
C:\pskill.exe -accepteula dllhost.exe
exit

Open in new window

and then run the .bat file instead of pskill directly. the exit command should now close the command window.
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
Why open a command prompt - why not just use:
wshShell.Run "C:\pskill.exe -accepteula dllhost.exe", 1, True

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oBdACommented:
Lee is correct, there's no need to run cmd.exe just to start another exe.
But in case you're interested, the issue is in line 31:
wshShell.Run "cmd /k C:\pskill.exe -accepteula dllhost.exe", 1, True

Open in new window

     
With the "/k", you're telling cmd.exe to run the command following, and then Keep the window open.
Replace the "/k" with a "/c", and the window will Close after execution of the command.
wshShell.Run "cmd /c C:\pskill.exe -accepteula dllhost.exe", 1, True

Open in new window

0
zalazarCommented:
Both solutions provided by Lee W. and oBda should work.
If it bothers you that you see all kind of popping command windows then also replace the "1" with 7. The Run method will then start the command window minimized and without activating it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

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.