{"id":64173,"date":"2007-08-23T01:40:00","date_gmt":"2007-08-23T01:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/23\/how-can-i-get-the-guid-for-a-user-account-if-all-i-have-is-the-users-logon-name-and-domain\/"},"modified":"2007-08-23T01:40:00","modified_gmt":"2007-08-23T01:40:00","slug":"how-can-i-get-the-guid-for-a-user-account-if-all-i-have-is-the-users-logon-name-and-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-the-guid-for-a-user-account-if-all-i-have-is-the-users-logon-name-and-domain\/","title":{"rendered":"How Can I Get the GUID for a User Account If All I Have is the User\u2019s Logon Name and Domain?"},"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 get the GUID for a user account if all I know is the user\u2019s logon name and domain?<BR><BR>&#8212; IU <\/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, IU. You know, one of the great things about the Scripting Guy who writes this column is \u2013 well, OK, right off the top of our head we can\u2019t think of <I>any<\/I> great things about the Scripting Guy who writes this column. (Although, no doubt the Scripting Editor will chime in with a list of great things. Scripting Editor, you there?) <\/P>\n<P>Nevertheless, the Scripting Guy who writes this column <I>is<\/I> pretty good at looking past outward appearances and getting right to the heart of a problem (admittedly, that\u2019s due in large part to the fact that he typically <I>is<\/I> the heart of a problem). When you get right down to it, IU, you have a translation problem: you\u2019re sitting there with a Windows NT-style domain name (e.g., fabrikam\\kenmyer), and you need to translate that to a user GUID (e.g., {97614339-087d-4f26-9fc5-86e8e7425ce7}. So here\u2019s a question for <I>you<\/I>: what do you need any time you have to translate something? That\u2019s exactly right: you need a translator.<\/P>\n<TABLE class=\"dataTable\" id=\"EKD\" 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>. You know, it\u2019s amazing insights such as that one \u2013 in order to translate something you need a translator \u2013 that make this column what it is. It <I>is<\/I> hard to believe that we give this stuff away for free, isn\u2019t it?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, there\u2019s just one problem here: there\u2019s simply now way to translate an NT 4 domain name to a GUID. No way at all.<\/P>\n<P>Or is there? Take a peek at this:<\/P><PRE class=\"codeSample\">Const ADS_NAME_INITTYPE_GC = 3\nConst ADS_NAME_TYPE_NT4 = 3\nConst ADS_NAME_TYPE_GUID = 7<\/p>\n<p>strUserName = &#8220;fabrikam\\kenmyer&#8221;<\/p>\n<p>Set objTranslator = CreateObject(&#8220;NameTranslate&#8221;)<\/p>\n<p>objTranslator.Init ADS_NAME_INITTYPE_GC, &#8220;&#8221;\nobjTranslator.Set ADS_NAME_TYPE_NT4, strUserName<\/p>\n<p>strUserGUID = objTranslator.Get(ADS_NAME_TYPE_GUID)<\/p>\n<p>Wscript.Echo strUserGUID\n<\/PRE>\n<P>We have to admit that this was actually kind of a fun little script to write; that\u2019s because it gave us a chance to use the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa706046.aspx\" target=\"_blank\"><B>IADsNameTranslate<\/B><\/A> utility, an ADSI interface that\u2019s built right into the operating system. As the name implies, the IADsNameTranslate interface can translate many of the different name types used in Active Directory. For example, give IADsNameTranslate a distinguished name and it can give you back a display name. Give IADsNameTranslate a canonical name and it can give you back a UPN name. Give IADsNameTranslate a Windows NT 4 domain name and it can give you back a GUID.<\/P>\n<P>Or so we hope, anyway. Let\u2019s find out.<\/P>\n<P>Our script begins by defining three constants:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ADS_NAME_INITTYPE_GC<\/B> (with a value of 3) tells the script that we want to look up name information by contacting a global catalog server.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ADS_NAME_TYPE_NT4<\/B> (also with a value of 3) is used to specify an NT 4 domain name.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ADS_NAME_TYPE_GUID<\/B> (with a value of 7) is used to specify an Active Directory GUID.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>After defining the constants we next assign the user name to a variable named strUserName, taking care to use the NT 4 domain naming convention <I>domain\\user name<\/I>:<\/P><PRE class=\"codeSample\">strUserName = &#8220;fabrikam\\kenmyer&#8221;\n<\/PRE>\n<P>Now we\u2019re ready to roll. For starters we create an instance of the <B>NameTranslate<\/B> object, the ProgID for the IADsNameTranslate interface. We then call the <B>Init<\/B> method to bind us to Active Directory; in this case, Init binds us to a global catalog server because we passed the constant ADS_NAME_INITTYPE_GC as the method parameter. (Note, too, that we also passed an empty string as the second parameter. This second parameter gives you an opportunity to specify a computer or domain name. We don\u2019t need to do that when we contact a global catalog server, but we <I>do<\/I> need to pass an empty string as this \u201coptional\u201d parameter. If we don\u2019t, the script will fail.)<\/P>\n<P>Incidentally, here\u2019s what our call to the Init method looks like:<\/P><PRE class=\"codeSample\">objTranslator.Init ADS_NAME_INITTYPE_GC, &#8220;&#8221;\n<\/PRE>\n<P>And here\u2019s what our call to the <B>Set<\/B> method looks like:<\/P><PRE class=\"codeSample\">objTranslator.Set ADS_NAME_TYPE_NT4, strUserName\n<\/PRE>\n<P>As you can see, we pass Set two parameters: the type of Active Directory name to be translated (represented by the constant ADS_NAME_TYPE_NT4) and the name itself (represented by the variable strUserName). After calling the Set method we then call the <B>Get<\/B> method. And what is it we want to get? That\u2019s easy: we want to get the user\u2019s GUID, something we indicate by passing the constant ADS_NAME_TYPE_GUID. The NameTranslate object will grab the GUID and stash it in a variable named strUserGUID:<\/P><PRE class=\"codeSample\">strUserGUID = objTranslator.Get(ADS_NAME_TYPE_GUID)\n<\/PRE>\n<P>So is that really going to work? Of course it is; here\u2019s what we see when we echo back the value of strUserGUID:<\/P><PRE class=\"codeSample\">{97614339-087d-4f26-9fc5-86e8e7425ce7}\n<\/PRE>\n<P>Pretty cool, huh?<\/P>\n<P>Like we said, there are other name formats you can work with as well, including the following:<\/P>\n<TABLE class=\"dataTable\" id=\"EZF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Description<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_1779<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Name format as specified in RFC 1779. For example, &#8220;CN=Jeff Smith,CN=users,DC=Fabrikam,DC=com&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_CANONICAL<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Canonical name format. For example, &#8220;Fabrikam.com\/Users\/Jeff Smith&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_NT4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Account name format used in Windows NT 4.0. For example, &#8220;Fabrikam\\JeffSmith&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_DISPLAY<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Display name format. For example, &#8220;Jeff Smith&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_DOMAIN_SIMPLE<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Simple domain name format. For example, &#8220;JeffSmith@Fabrikam.com&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_ENTERPRISE_SIMPLE<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Simple enterprise name format. For example, &#8220;JeffSmith@Fabrikam.com&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_GUID<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">7<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Global Unique Identifier format. For example, &#8220;{95ee9fff-3436-11d1-b2b0-d15ae3ac8436}&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_USER_PRINCIPAL_NAME<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">9<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">User principal name format. For example, &#8220;JeffSmith@Fabrikam.com&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_CANONICAL_EX<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">10<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Extended canonical name format. For example, &#8220;Fabrikam.com\/Users Jeff Smith&#8221;.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ADS_NAME_TYPE_SERVICE_PRINCIPAL_NAME<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">11<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Service principal name format. For example, &#8220;www\/www.fabrikam.com@fabrikam.com&#8221;.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Give some of those a try and see what happens. We haven\u2019t discussed the IADsNameTranslate interface much, but it\u2019s a handy little thing to know about. On top of that, you can make a good case that this approach is a little faster and a little easier to use than an Active Directory search script, which would be an alternate method for looking up a specific user and then returning his or her GUID. <\/P>\n<P>We hope that helps you with your translation problem, IU. Having recently returned from a trip to Italy, the Scripting Guy who writes this column knows all about translation issues. Not because of his trip to Italy: not only do a large number of Italians speak English, but the Scripting Guy who writes this column learned enough Italian to at least read menus and stuff. Instead, the Scripting Guy who writes this column needs a translator now that he\u2019s back at <I>Microsoft<\/I>, listening to people say things like Web 2.0; strategic alignment; mission statements; empowerment and immersion; and proactive synergy. <\/P>\n<P>Not to mention our all-time favorite: campus-wide infusion. (Which turns out to be the activity you engage in any time you hang up a poster in a Microsoft building.) <\/P>\n<P>Ah, it\u2019s good to be back!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get the GUID for a user account if all I know is the user\u2019s logon name and domain?&#8212; IU Hey, IU. You know, one of the great things about the Scripting Guy who writes this column is \u2013 well, OK, right off the top of our head we can\u2019t [&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":[7,3,20,5],"class_list":["post-64173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get the GUID for a user account if all I know is the user\u2019s logon name and domain?&#8212; IU Hey, IU. You know, one of the great things about the Scripting Guy who writes this column is \u2013 well, OK, right off the top of our head we can\u2019t [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64173","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=64173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64173\/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=64173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}