{"id":69723,"date":"2005-05-26T19:06:00","date_gmt":"2005-05-26T19:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/26\/how-can-i-get-just-the-user-name-when-using-the-win32_computersystem-class\/"},"modified":"2005-05-26T19:06:00","modified_gmt":"2005-05-26T19:06:00","slug":"how-can-i-get-just-the-user-name-when-using-the-win32_computersystem-class","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-just-the-user-name-when-using-the-win32_computersystem-class\/","title":{"rendered":"How Can I Get Just the User Name When Using the Win32_ComputerSystem Class?"},"content":{"rendered":"<p><P>&nbsp;<\/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! I\u2019m using the Win32_ComputerSystem class to get the name of the logged-on user. The name comes back formatted like this: <I>Domain\\Name<\/I>. How can I extract just the user name?<BR><BR>&#8212; KW<\/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, KW. As our much-beloved and highly-respected editor reminded us the other day, instead of spending all our time telling people how to <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may05\/hey0525.mspx\"><B>extract paragraphs formatted with a specific style from Word documents<\/B><\/A> we promised that every now and then we\u2019d take time to address more basic questions. Having made that promise we then just as quickly forgot about it. (But, in our defense, it\u2019s awfully hard to resist the thrill and excitement that comes with extracting paragraphs formatted with a specific style from Word documents.) <\/P>\n<P>So we\u2019ll make amends today. As many of you know, you can use a simple little script like this to determine the user currently logged-on to 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;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_ComputerSystem&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.UserName\nNext\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EED\" 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>. One caveat. This works pretty good on Windows XP and Windows Server 2003. On Windows 2000, however, it always returns Null unless an administrator is logged on to the computer. Therefore, this is not the answer to the age-old question, \u201cHow can I determine who\u2019s logged on to a particular computer?\u201d (Unless, of course, all your users happen to be local administrators.) Unfortunately, there\u2019s really not a good answer to that question.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As KW noted, though, the value of the <B>UserName<\/B> property comes back looking like this:<\/P><PRE class=\"codeSample\">FABRIKAM\\kenmyer\n<\/PRE>\n<P>That\u2019s fine if you want both the domain name (FABRIKAM) and the user name (kenmyer). But what if you want only the user name? How can you separate the user name from the domain name?<\/P>\n<P>Believe it or not, this is pretty easy. With the UserName property the domain name and the user name will always be separated by a \\. This, by the way, is true even if the user is logged on locally; in that case the <I>computer<\/I> name and the user name will be separated by a \\:<\/P><PRE class=\"codeSample\">ATL-WS-01\\kenmyer\n<\/PRE>\n<P>Because of that we can use the VBScript <B>Split<\/B> function to split the UserName into an array; all we have to do is tell the Split function to split the name at the \\. That will give us an array consisting of two pieces: the domain name and the user name. All we have to do then is echo back the second piece &#8211; the user name &#8211; and we\u2019re in business.<\/P>\n<P>Confused? Let\u2019s take a look at the script and then we\u2019ll walk you through it:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_ComputerSystem&#8221;)<\/p>\n<p>For Each objItem in colItems\n    arrName = Split(objItem.UserName, &#8220;\\&#8221;)\n    Wscript.Echo &#8220;Domain: &#8221; &amp; arrName(0)\n    Wscript.Echo &#8220;Name: &#8221; &amp; arrName(1)\nNext\n<\/PRE>\n<P>We begin by connecting to the WMI service on the local computer. (Although we could just as easily connect to the WMI service on a remote computer. To do that just change the value of the variable strComputer to the name of the remote computer.) We then use the <B>ExecQuery<\/B> method to retrieve all instances of the Win32_ComputerSystem class (there will be only one such instance).<\/P>\n<P>Next we set up a For Each loop to walk through all the instances of the Win32_ComputerSystem class. In the first script we showed you &#8211; the one that echoed both the domain name and the user name &#8211; all we did in this loop was echo the value of the UserName property. This time we\u2019re going to do things a little different: we\u2019re going to use the Split function to create an array out of the UserName property. This line of code splits the UserName value at the \\ mark, and creates an array named arrName:<\/P><PRE class=\"codeSample\">arrName = Split(objItem.UserName, &#8220;\\&#8221;)\n<\/PRE>\n<P>Incidentally, the Split function will split a string as many times as needed; that is, as many times as it finds the target character (in this case, the \\). For example, suppose we had the following string:<\/P><PRE class=\"codeSample\">element_1\\element_2\\element_3\\element_4\n<\/PRE>\n<P>If we run the Split function against this string our resulting array will actually have four elements:<\/P><PRE class=\"codeSample\">element_1\nelement_2\nelement_3\nelement_4\n<\/PRE>\n<P>With the UserName property we\u2019ll end up with a two-element array, something like this:<\/P><PRE class=\"codeSample\">FARBRIKAM\nkenmyer\n<\/PRE>\n<P>In an array, the first element (FABRIKAM) is always element 0; the second element (kenmyer) is always element 1. To echo back the domain name, we simply echo back the value of element 0; to echo back the user name, we simply echo back the value of element 1. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Domain: &#8221; &amp; arrName(0)\nWscript.Echo &#8220;Name: &#8221; &amp; arrName(1)\n<\/PRE>\n<P>That\u2019s all we have to do: we can now echo back just the domain name, just the user name, or both. And best of all, our editor will quit bugging us about addressing basic scripting questions.<\/P>\n<P>At least for awhile anyway.<\/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\/may05\/hey0526.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\/may05\/hey0526.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I\u2019m using the Win32_ComputerSystem class to get the name of the logged-on user. The name comes back formatted like this: Domain\\Name. How can I extract just the user name?&#8212; KW Hey, KW. As our much-beloved and highly-respected editor reminded us the other day, instead of spending all our time telling people [&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":[237,31,3,5],"class_list":["post-69723","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-computer-information","tag-operating-system","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I\u2019m using the Win32_ComputerSystem class to get the name of the logged-on user. The name comes back formatted like this: Domain\\Name. How can I extract just the user name?&#8212; KW Hey, KW. As our much-beloved and highly-respected editor reminded us the other day, instead of spending all our time telling people [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69723","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=69723"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69723\/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=69723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}