title: VBScript to check for open process by user
body: <p>I need a VBScript that will check if a process is in use by a specific user:</p>
  
  <p>Agent clicks program icon --> batch file calls for progcheck.vbs --> </p>
  
  <p>progcheck.vbs looks to see is "whatever.exe" is running under that user only --></p>
  
  <p>if program is running under that user then MsgBox "Program running" --> wscript.quit (this needs to terminate out of the batch file)</p>
  
  <p>else --> return to batch file.</p>
  
  <p>I have tried this with tasklist in a batch file and the script works, but takes forever to run for a domain user.  Want to do this in vbscript anyway.</p>
  
  <p><em><strong></em>***<em></strong> UPDATED SCRIPT WITH MODS 10/12 <strong></em>**<em>*</em>**</strong></p>
  
  <pre><code>OPTION EXPLICIT
  
      DIM strComputer,strProcess, strUserName,wshShell
  
      Set wshShell = WScript.CreateObject( "WScript.Shell" )
      strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
  
      strComputer = "." ' 
      strProcess = "notepad.exe"
  
  
      IF isProcessRunning(strComputer,strProcess,strUserName) THEN
              If MsgBox ("Notepad needs to be closed.", 1) = 1 then
                  wscript.Quit(1)
              End If
      END IF
  
  FUNCTION isProcessRunning(BYVAL strComputer,BYVAL strProcessName,BYVAL strUserName)
  
      DIM objWMIService, strWMIQuery
  
      strWMIQuery = "Select * from Win32_Process where name like '" &amp; strProcessName &amp; "' AND owner like '" &amp;strUserName&amp; "'" 
  
      SET objWMIService = GETOBJECT("winmgmts:" _
          &amp; "{impersonationLevel=impersonate}!\\" _ 
              &amp; strComputer &amp; "\root\cimv2") 
  
  
      IF objWMIService.ExecQuery(strWMIQuery).Count &gt; 0 THEN
          isProcessRunning = TRUE
      ELSE
          isProcessRunning = FALSE
      END If
  
  End Function
  </code></pre>
  
  <p>Let me know what you think and where I have it wrong.  Thanks in advance.</p>