{"id":68853,"date":"2005-09-29T19:35:00","date_gmt":"2005-09-29T19:35:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/29\/how-can-i-determine-the-logon-name-for-a-user\/"},"modified":"2005-09-29T19:35:00","modified_gmt":"2005-09-29T19:35:00","slug":"how-can-i-determine-the-logon-name-for-a-user","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-logon-name-for-a-user\/","title":{"rendered":"How Can I Determine the Logon Name for a User?"},"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! How can I determine the user logon name for a user named John Smith?<BR><BR>&#8212; FR<\/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, FR. You know, we\u2019d <I>like<\/I> to tell you how to determine the user logon name for a user named John Smith, but we can\u2019t: that\u2019s because we have a specific list of names representing the <I>only<\/I> user names we can reference in one of our sample scripts. Unfortunately, John Smith isn\u2019t on that list, so we can\u2019t use his name.<\/P>\n<P>Yeah, we feel bad about that, too, but our hands are tied. But tell you what: how about we show you a script that determines the user logon name for a user named Ken Myer? Yes, we know: it\u2019s really <I>not<\/I> the same, is it? But it\u2019s the best we can do:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT sAMAccountName FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;user&#8217; &#8221; &amp; _\n        &#8220;AND givenName=&#8217;Ken&#8217; AND sn=&#8217;Myer'&#8221;\nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;sAMAccountName&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>As you probably figured out, this is a script that searches Active Directory. We\u2019re not going to explain each and every line of code used in this script; that would take too long. If you\u2019re not familiar with Active Directory search scripts we encourage you to take a look at our two-part <I>Tales from the Script <\/I>series <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude: Where\u2019s My Printer?<\/B><\/A> All the crazy-looking stuff you see in this script &#8211; ADsDSOObject, ADS_SCOPE_SUBTREE, ADODB.Command &#8211; is explained in detail in those two columns.<\/P>\n<P>We will, however, point out a few things regarding the query used to conduct the search. Probably the hardest part about writing scripts that search Active Directory is knowing the property names to search for. For example, you referred to the user logon name. We know what you mean by that and you know what you mean by that, but Active Directory has no idea what a user logon name is. Instead, Active Directory calls that same thing the <B>sAMAccountName<\/B>. (<B>Note<\/B>: Although the letter casing doesn\u2019t matter, we write this property name out as <I>sAMAccountName<\/I> simply because that\u2019s the official name for the attribute.) Consequently, our SQL query retrieves the sAMAccountName for the specified user.<\/P>\n<P>And how do we specify that user? Well, we\u2019re looking for an Active Directory object that meets three criteria:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Is a user account. To limit returned data to user accounts we search for items where the <B>objectCategory<\/B> is equal to <I>user<\/I>.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Has a first name of <I>Ken<\/I>. Of course, Active Directory doesn\u2019t know what a first name is. Therefore, we need to search for users with a <B>givenName<\/B> of <I>Ken<\/I>.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Has a last name of <I>Myer<\/I>. As you might expect, Active Directory has never heard the term \u201clast name,\u201d either. Instead, we need to search for the <B>sn<\/B> (surname) <I>Myer<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Add that all together, and we end up with a query that looks like this:<\/P><PRE class=\"codeSample\">objCommand.CommandText = _\n    &#8220;SELECT sAMAccountName FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;user&#8217; &#8221; &amp; _\n        &#8220;AND givenName=&#8217;Ken&#8217; AND sn=&#8217;Myer'&#8221;\n<\/PRE>\n<P>The rest is easy. We execute the query, and Active Directory returns a recordset consisting of all the users with a giveName of <I>Ken<\/I> and an sn of <I>Myer<\/I>. We then set up a Do Until loop to walk through the recordset, echoing back the sAMAccountName for each user. (Ideally there will be only one Ken Myer in Active Directory, but you could have more than one user with the same first and last name. In that case the sAMAccountName will be the differentiating factor, because sAMAccountNames <I>must<\/I> be unique.)<\/P>\n<P>Does that help? OK, look, don\u2019t tell anyone we said this, but take the script we just showed you, replace <I>Ken<\/I> with <I>John<\/I> and <I>Myer<\/I> with <I>Smith<\/I> and you\u2019ll have a script that searches for a user named John Smith. But that\u2019s just between you and us, OK? OK.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the user logon name for a user named John Smith?&#8212; FR Hey, FR. You know, we\u2019d like to tell you how to determine the user logon name for a user named John Smith, but we can\u2019t: that\u2019s because we have a specific list of names representing the only [&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,8,5],"class_list":["post-68853","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine the user logon name for a user named John Smith?&#8212; FR Hey, FR. You know, we\u2019d like to tell you how to determine the user logon name for a user named John Smith, but we can\u2019t: that\u2019s because we have a specific list of names representing the only [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68853","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=68853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68853\/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=68853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}