I have a requirement. I need to check if a process is running. If it does, prompt the user to close it. If the user doesnt close it in 10 seconds, force close it and continue with installation. So to tackle this, i used some reference and came up with following

Dim i

Dim strComputer,oShell,intReturn,oFSO,SysFolder

Dim FindProc

Const wshYes = 6

Const wshNo = 7

Const wshYesNoDialog = 4

Const wshQuestionMark = 32


strComputer = "."

Set oShell = CreateObject("Wscript.Shell")

Set oFSO=createObject("Scripting.FileSystemObject")


'FindProc = inputbox("Enter process name","Information required")



Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _

    ("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")


If colProcessList.count>0 then

    'wscript.echo FindProc & " is running"

intReturn = oShell.Popup("Notepad is running. Please close it and then click Yes",10, "Information",wshYesNoDialog + wshQuestionMark)


If intReturn=wshYes Then

oFSO.CreateFolder(SysFolder & "\Test")


For Each objProcess in colProcessList 



 oFSO.CreateFolder(SysFolder & "\Test")

End If


    wscript.echo FindProc & " is not running"

End if 


Set objWMIService = Nothing

Set colProcessList = Nothing

Set oShell = Nothing 


Now , those having a good hold on scripting will make it out at first instance. When user clicks yes, there is no way to check if the process is still running. 
Would really appreciate if someone can help me out with this looping 

3 Comments   [ - ] Hide Comments


  • You can use WScript.Sleep(10000) in your script to wait for 10 sec.
    then terminate the process.

    If you want to wait for process to get terminated and check again for its running status then then looping has to be done.



    let me know if you need more help/information
    • rock_star: thanks for the help brother and the #2 link worked wonder.
  • You should move the PopUp inside the loop. However, I would create a function with the process test and termination and call that inside a loop with a Yes/No/Cancel option. I would also add a test to make sure that the process did indeed get terminated, as many apps allow multiple instances to run.
    • Thanks for the solution VBScab. Wouldn't have bothered you'll, but this was the first time i got a bit stumped with vbscript. But i appreciate your response. Thank you ......all of you
  • Could you post your final script, potentially with comments on the feedback shared? Would be useful to have that for others I'd think :)
    • set oshell=createobject("wscript.shell")
      strMessage="Close the notepad"

      strComputer = "."
      strComputer = "."
      Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

      oreturn=oshell.popup("Do you want to start the installation",0,"Information",&H0 + &H40)

      Set colProcessList = objWMIService.ExecQuery _
      ("Select * from Win32_Process Where Name = 'notepad.exe'")
      If colProcessList.Count>0 Then
      ortn=oshell.popup(strMessage,0,strTitle,&H0 + &H40)
      End If
      Loop While colProcessList.Count>0

      This is the final script i came up with. Hope this helps others too
Please log in to comment

There are no answers at this time
Answer this question or Comment on this question for clarity