{"id":70743,"date":"2005-01-03T11:04:00","date_gmt":"2005-01-03T11:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/03\/how-can-i-determine-the-account-a-process-is-running-under\/"},"modified":"2005-01-03T11:04:00","modified_gmt":"2005-01-03T11:04:00","slug":"how-can-i-determine-the-account-a-process-is-running-under","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-account-a-process-is-running-under\/","title":{"rendered":"How Can I Determine the Account a Process is Running Under?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I\u2019ve got a script that returns information about all the processes running on a computer, except I can\u2019t seem to figure out how to get the name of the user account that these processes are running under. Can you help?<BR><BR>&#8212; DL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DL. Yes, we can help. It\u2019s actually fairly easy to determine which account a process is running under, it\u2019s just not very obvious how you go about doing that. If you\u2019re like most people, you probably scanned the properties for the Win32_Process class trying to find a property named Account or UserName or something similar. Most likely you didn\u2019t find it. And there\u2019s a good reason for that: the Win32_Process doesn\u2019t <I>have<\/I> a property that tells you which account a process is running under.<\/P>\n<P>Instead, you need to use a method &#8211; GetOwner &#8211; to track down this information. Here\u2019s a script that tells you which account Microsoft Word (Winword.exe) is running under:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colProcessList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where Name = &#8216;Winword.exe'&#8221;)<\/p>\n<p>For Each objProcess in colProcessList\n    objProcess.GetOwner strUserName, strUserDomain \n    Wscript.Echo &#8220;Process &#8221; &amp; objProcess.Name &amp; &#8221; is owned by &#8221; _ \n        &amp; strUserDomain &amp; &#8220;\\&#8221; &amp; strUserName &amp; &#8220;.&#8221;\nNext\n<\/PRE>\n<P>The line of code we\u2019re most interested in is this one:<\/P><PRE class=\"codeSample\">objProcess.GetOwner strNameOfUser, strUserDomain\n<\/PRE>\n<P>What we\u2019re doing here is calling the <B>GetOwner<\/B> method. GetOwner returns two \u201cout parameters,\u201d one that returns the name of the user responsible for the process, the other returning the domain that user belongs to. In order to capture these two out parameters we need to supply the GetOwner method with two variables. In this sample script, we\u2019ve used variables named strUserName and strUserDomain. The names are arbitrary; you can call the variables A and B or X and Y or anything you want. <\/P>\n<P>However, the order of the variables is <I>not<\/I> arbitrary: the first value returned will always be the user name, the second value will always will be the domain. Which means that if you want X to represent the user name and Y to represent the domain, then make sure your code looks like this:<\/P><PRE class=\"codeSample\">objProcess.GetOwner X, Y\n<\/PRE>\n<P>After calling GetOwner we simply echo back the process name and the owner. Notice that &#8211; to be a little fancy &#8211; we use the domain\\user format; that way, we echo a name like <B>fabrikam\\kenmyer<\/B>.<\/P>\n<P>Incidentally, here\u2019s a script that lists all the processes on a computer as well as the owner of each process:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colProcessList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process&#8221;)<\/p>\n<p>For Each objProcess in colProcessList\n    objProcess.GetOwner strUserName, strUserDomain\n    Wscript.Echo &#8220;Process &#8221; &amp; objProcess.Name &amp; &#8221; is owned by &#8221; _ \n        &amp; strUserDomain &amp; &#8220;\\&#8221; &amp; strUserName &amp; &#8220;.&#8221;\nNext\n<\/PRE>\n<P>Oh, and in case anyone is wondering, January 3, 2005 happens to be an official day off for Microsoft employees. So why is there a <I>Hey, Scripting Guy! <\/I>column today? Well, that can only be because of the incredible dedication and devotion to duty shown by the Microsoft Scripting Guys. Either that, or one of the Scripting Guys &#8211; who shall remain nameless &#8211; didn\u2019t realize it was a holiday and came in anyway (and at 7:00 AM to boot!).<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019ve got a script that returns information about all the processes running on a computer, except I can\u2019t seem to figure out how to get the name of the user account that these processes are running under. Can you help?&#8212; DL Hey, DL. Yes, we can help. It\u2019s actually fairly easy to [&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-70743","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! I\u2019ve got a script that returns information about all the processes running on a computer, except I can\u2019t seem to figure out how to get the name of the user account that these processes are running under. Can you help?&#8212; DL Hey, DL. Yes, we can help. It\u2019s actually fairly easy to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70743","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=70743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70743\/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=70743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}