{"id":67203,"date":"2006-06-01T15:29:00","date_gmt":"2006-06-01T15:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/01\/how-can-i-find-the-process-id-associated-with-a-batch-file\/"},"modified":"2006-06-01T15:29:00","modified_gmt":"2006-06-01T15:29:00","slug":"how-can-i-find-the-process-id-associated-with-a-batch-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-find-the-process-id-associated-with-a-batch-file\/","title":{"rendered":"How Can I Find the Process ID Associated with a Batch File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I find the process ID associated with a batch file?<BR><BR>&#8212; MB<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, MB. You know, the Scripting Guys are firm believers in recycling. Why throw that printout away when it can be recycled into newsprint? Why throw that aluminum can away when it can be melted down and reused? Why throw away that <I>Hey, Scripting Guy!<\/I> column when you can use it to answer someone else\u2019s question a few months later?<\/P>\n<P>In other words, today just happens to be Recycling Day here in the Script Center. A long time ago (October 6, 2004, to be precise) we <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1006.mspx\"><B>wrote a column<\/B><\/A> telling people how they could determine which scripts (by file name) were running on a computer. Today we\u2019re going to recycle that column, using a slight variation to show you how you can determine the process ID associated with any batch files that are running on your computer. Some of the stuff we\u2019ll show you today is new, but one thing that <I>hasn\u2019t<\/I> changed is the disclaimer: this solution works only on Windows XP or Windows Server 2003. (We\u2019ll explain why in a bit.) And, no: we don\u2019t really know of a good way to do this on any earlier version of Windows. Sorry.<\/P>\n<P>Assuming you\u2019re running Windows XP or Windows Server 2003, however, the following script reports back the name and process ID of any batch file running on a computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_Process&#8221;)<\/p>\n<p>For Each objItem in colItems\n    If InStr(objItem.CommandLine, &#8220;.bat&#8221;) Or InStr(objItem.CommandLine, &#8220;.cmd&#8221;) Then\n        Wscript.Echo &#8220;Batch file: &#8221; &amp; objItem.CommandLine\n        Wscript.Echo &#8220;Process ID: &#8221; &amp; objItem.ProcessID\n    End If\nNext\n<\/PRE>\n<P>So how does this script work? Good question. To begin with, we bind to the WMI service on the local computer. (Although we could easily adapt the script to retrieve the process IDs on any batch files running on a remote computer; all we\u2019d have to do is set the value of the variable strComputer to the name of that remote computer.) After making the connection we then use this line of code to retrieve a collection of all the processes running on the computer:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_Process&#8221;)\n<\/PRE>\n<P>So far so good, right? Next we set up a For Each loop to loop through the collection of processes. Inside that loop we use the <B>InStr<\/B> method to see if the strings <I>.bat <\/I>or <I>.cmd<\/I> can be found anywhere within the value of the <B>CommandLine<\/B> property, which simply reports back the command line value used to start a process. (What if you started the process by double-clicking an icon in Windows Explorer? No problem; in that case CommandLine is equal to the command line value you <I>would<\/I> have used had you started the process from the command line or from the <B>Run<\/B> dialog box.) <\/P>\n<P>For example, here\u2019s the kind of information you can expect to see when you look at the value of the CommandLine property:<\/P><PRE class=\"codeSample\">cmd \/c &#8220;&#8221;C:\\Scripts\\test.cmd&#8221; &#8221;\n&#8220;C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE&#8221;\n&#8220;C:\\WINDOWS\\system32\\NOTEPAD.EXE&#8221; C:\\Scripts\\cmdline.vbs\n<\/PRE>\n<P>If you take a look at the first process you can see that we have a batch file named Test.cmd running. By looking for processes where the CommandLine property includes the strings <I>.bat<\/I> or <I>.cmd<\/I> (the file extensions used for batch files) we can determine whether any of these processes are actually running batch files. And once we know that, getting the process ID for those batch files is trivial.<\/P>\n<TABLE class=\"dataTable\" id=\"EJE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, you\u2019re right: a more precise test would be to check and see if the last four characters of the path name are .bat or .cmd. We didn\u2019t do that because the double quotes tacked on the end of the CommandLine value &#8212; cmd \/c &#8220;&#8221;C:\\Scripts\\test.cmd<B>&#8221; &#8220;<\/B> &#8211; complicate things a bit. We\u2019re assuming that, in the vast majority of cases, any time you have a .bat or a .cmd in the path then you have a batch file. But if you want to improve upon what we\u2019ve done here, well, who are we to stop you?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In case you\u2019re wondering, this also explains why our script works only on Windows XP and Windows Server 2003: that\u2019s because the CommandLine property is available on only those two platforms.<\/P>\n<P>So what if we <I>do<\/I> find a batch file? In that case we simply use these two lines of code to echo back the process name and the process ID:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Batch file: &#8221; &amp; objItem.CommandLine\nWscript.Echo &#8220;Process ID: &#8221; &amp; objItem.ProcessID\n<\/PRE>\n<P>And there you have it.<\/P>\n<P>Of course, we should point out that, as written, this script returns the name and process ID of <I>all<\/I> the batch files running on a computer. In your question, MB, you implied that you were interested in only a <I>particular<\/I> batch file. If that\u2019s the case, then simply modify the If-Then statement so it looks for a CommandLine containing the full batch file name. For example, this modified script checks to see if any processes have the string <I>Test.cmd<\/I> somewhere in the CommandLine property:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_Process&#8221;)<\/p>\n<p>For Each objItem in colItems\n    If InStr(objItem.CommandLine, &#8220;test.cmd&#8221;) Then\n        Wscript.Echo &#8220;Process ID: &#8221; &amp; objItem.ProcessID\n    End If\nNext\n<\/PRE>\n<P>If found, the script reports back the process ID for only the specified batch file.<\/P>\n<P>Want to get <I>really<\/I> fancy? Here\u2019s a monitoring script that alerts you any time a new batch file starts up:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colMonitoredProcesses = objWMIService. _        \n    ExecNotificationQuery(&#8220;select * from __instanceCreationEvent &#8221; _ \n        &amp; &#8221; within 1 where TargetInstance isa &#8216;Win32_Process'&#8221;)\ni = 0<\/p>\n<p>Do While i = 0\n    Set objProcess = colMonitoredProcesses.NextEvent\n    If InStr(objProcess.TargetInstance.CommandLine, &#8220;.cmd&#8221;) Or _\n        InStr(objProcess.TargetInstance.CommandLine, &#8220;.bat&#8221;) Then \n        Wscript.Echo &#8220;Batch file: &#8221; &amp; objProcess.TargetInstance.CommandLine\n        Wscript.Echo &#8220;Process ID: &#8221; &amp; objProcess.TargetInstance.ProcessID\n    End If\nLoop\n<\/PRE>\n<P>We won\u2019t discuss this script in any detail today; for more information see the webcast <A href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032268754&amp;culture=en-US\" target=\"_blank\"><B>An Ounce of Prevention: An Introduction to WMI Events<\/B><\/A>. (And yes, we <I>are<\/I> recycling old material again, this time an old Scripting Week 2 webcast. Didn\u2019t we mention that today was Recycling Day?)<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0601.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0601.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I find the process ID associated with a batch file?&#8212; MB Hey, MB. You know, the Scripting Guys are firm believers in recycling. Why throw that printout away when it can be recycled into newsprint? Why throw that aluminum can away when it can be melted down and reused? Why [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,3,5],"class_list":["post-67203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I find the process ID associated with a batch file?&#8212; MB Hey, MB. You know, the Scripting Guys are firm believers in recycling. Why throw that printout away when it can be recycled into newsprint? Why throw that aluminum can away when it can be melted down and reused? Why [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67203\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}