{"id":67463,"date":"2006-04-25T16:49:00","date_gmt":"2006-04-25T16:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/25\/how-can-i-determine-the-name-of-the-local-computer-using-wmi\/"},"modified":"2006-04-25T16:49:00","modified_gmt":"2006-04-25T16:49:00","slug":"how-can-i-determine-the-name-of-the-local-computer-using-wmi","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-name-of-the-local-computer-using-wmi\/","title":{"rendered":"How Can I Determine the Name of the Local Computer Using WMI?"},"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 determine the name of the local computer using WMI?<BR><BR>&#8212; BM<\/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, BM. As you noted in your email we Scripting Guys always tell people that, in WMI, you can simply use a dot (.) to refer to the local computer. For example, the following mini-script connects you to the WMI service on the local computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>Cool, huh? Of course, as you pointed out that\u2019s really only half the story: yes, we can use a dot to refer to the local computer, but what if we need to know the <I>name<\/I> of that computer? When it comes to doing that, when it comes to helping you determine the name of the machine that the dot represents, well, in that case the Scripting Guys haven\u2019t been much help.<\/P>\n<TABLE class=\"dataTable\" id=\"E5C\" 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>. To be honest, it doesn\u2019t really sound like the Scripting Guys to do half a job and then just plain give up on it. Usually we don\u2019t make it even <I>that<\/I> far before giving up on something!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Before we go any further we should probably take a moment to address the skeptics in the audience. No doubt some of you are looking at this question and thinking, \u201cSo what\u2019s the big deal here? If the script is running on the local computer, well, then why would you even care about the name of the local computer? And if the script is running remotely, well, wouldn\u2019t you already <I>know<\/I> the name of the computer where the script is running? Wouldn\u2019t you simply set the value of the variable strComputer to the name of that remote machine?\u201d<\/P>\n<P>The answer to all those questions is the same: maybe. But, then again, maybe not. For example, suppose our code is running as part of a logon script or a computer startup script, and suppose we need to log collected data to a database. In one of those scenarios we need to know the name of the computer. Yes, the script will run just fine by using a dot to represent the computer name, but we can\u2019t rely entirely on the dot; if we do, our database will end up looking something like this:<\/P>\n<TABLE class=\"dataTable\" id=\"ESD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Computer<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Number of Processors<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">.<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">.<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">.<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">.<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Cute, but not too terribly informative. Needless to say, a table like this would be a bit more useful:<\/P>\n<TABLE class=\"dataTable\" id=\"EXE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Computer<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Number of Processors<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">atl-ws-01<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">atl-fs-02<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">atl-dc-03<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">atl-ws-04<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Likewise there might be times when we <I>do<\/I> need get the computer name when dealing with remote machines. With WMI you can connect to a remote computer by specifying the computer name; however, you can also connect to a remote computer by specifying an IP address:<\/P><PRE class=\"codeSample\">strComputer = &#8220;192.168.1.1&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>Because very few of us have memorized the IP addresses of all our computers (and because, thanks to DHCP, many of these IP addresses are wont to change anyway) a table like is also only marginally useful:<\/P>\n<TABLE class=\"dataTable\" id=\"ECG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Computer<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Number of Processors<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">192.168.1.1<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">192.168.1.2<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">192.168.1.3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">192.168.1.4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In other words, even in this high-tech day and age of ours something as old-fashioned as a name can still come in handy. (Which sounds funny coming from a group that hides behind the collective moniker \u201cThe Scripting Guys,\u201d doesn\u2019t it?)<\/P>\n<P>So can we use WMI to determine the name of the local computer (or, say, a remote machine where all we know is the IP address)? You bet we can. And when we tell you that there are hundreds and hundreds of different ways to do that, well, we aren\u2019t kidding. For example, suppose you have a burning desire to use the <B>Win32_BIOS<\/B> class to determine the computer name. Hey, why not:<\/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_BIOS&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Path_.Server\nNext\n<\/PRE>\n<P>As you can see, this is a standard WMI script, one that binds to the WMI service on the local computer. We then use this line of code to retrieve BIOS information for that machine:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_BIOS&#8221;)\n<\/PRE>\n<P>Why did we pick the Win32_BIOS class? No reason; we could have picked <I>any<\/I> WMI class. That\u2019s because each and every WMI class returns an SWbemObjectPath object; the <B>Path_<\/B> property for that object serves as the unique address for the item in question. For example, the Path_ to the BIOS on a computer might look something like this:<\/P><PRE class=\"codeSample\">\\\\ATL-WS-01\\root\\cimv2:Win32_BIOS.Name=&#8221;EPP runtime BIOS &#8211; Version 1.1 &#8220;, \nSoftwareElementID=&#8221;EPP runtime BIOS &#8211; Version 1.1 &#8220;,SoftwareElementState=3,\nTargetOperatingSystem=0,Version=&#8221;HP     &#8211; 22110520&#8221;\n<\/PRE>\n<P>That path is unique: no other object on the computer atl-ws-01 will have that same path. <\/P>\n<P>As you can see, the name of the computer (ATL-WS-01) is embedded in the beginning portion of that path. (Which makes sense: not including the name of the computer in the path would be about as useful as having an address of 111 Main Street that didn\u2019t include the name of the city.) Admittedly, we could do some fancy string manipulation in order to try and tease out the computer name from the path. Or, we could simply ask for the value of the <B>Server<\/B> property, like we do here:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n    Wscript.Echo objItem.Path_.Server\nNext\n<\/PRE>\n<P>It\u2019s that easy. And, best of all, any WMI object you get back from a script will include an SWbemObjectPath object and the Server property. Are you already using the Win32_ParallelPort class to connect to a machine? That\u2019s fine; while you\u2019re there you can get the value of the Server property using Win32_ParallelPort:<\/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_ParallelPort&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Path_.Server\nNext\n<\/PRE>\n<P>Connecting to a machine using the Win32_PrinterConfiguration class? Then you can get at the value of the Server property using Win32_PrinterConfiguration. Connecting to a machine using the Win32_LogicalDisk class? Then you can get at &#8211; well, you get the idea.<\/P>\n<P>But what if you don\u2019t like all this talk of SWbemObjectPaths? Well, in that case you can always rely on the Win32_ComputerSystem class to get the computer name:<\/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.Name\nNext\n<\/PRE>\n<P>That works just fine; it\u2019s just nowhere near as cool as using the Win32_TimeZone class to determine the computer name. And just think of how many bar bets you\u2019re likely to win now; after all, from here on you\u2019ll be able to walk into a crowded bar and tell people, \u201cGive me the name of a WMI class &#8211; any WMI class &#8211; and I\u2019ll use <I>just that class<\/I> to determine the name of a computer.\u201d If people aren\u2019t impressed by that then maybe they aren\u2019t worth impressing.<\/P>\n<P>We hope that answers your question, BM. After all, we Scripting Guys take pride in our motto: Leave no job undone. We don\u2019t necessarily live up to that motto, mind you. But we do take pride in it.<\/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\/apr06\/hey0425.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\/apr06\/hey0425.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the name of the local computer using WMI?&#8212; BM Hey, BM. As you noted in your email we Scripting Guys always tell people that, in WMI, you can simply use a dot (.) to refer to the local computer. For example, the following mini-script connects you to the [&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":[3,4,5,6],"class_list":["post-67463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine the name of the local computer using WMI?&#8212; BM Hey, BM. As you noted in your email we Scripting Guys always tell people that, in WMI, you can simply use a dot (.) to refer to the local computer. For example, the following mini-script connects you to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67463","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=67463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67463\/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=67463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}