{"id":69013,"date":"2005-09-07T22:32:00","date_gmt":"2005-09-07T22:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/07\/how-can-i-check-to-see-if-a-user-exists-in-an-nt-4-domain\/"},"modified":"2005-09-07T22:32:00","modified_gmt":"2005-09-07T22:32:00","slug":"how-can-i-check-to-see-if-a-user-exists-in-an-nt-4-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-check-to-see-if-a-user-exists-in-an-nt-4-domain\/","title":{"rendered":"How Can I Check to See if a User Exists in an NT 4 Domain?"},"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 check to see if a user exists in an NT 4 domain?<BR><BR>&#8212; PA<\/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, PA. You know, if you were to ask us, \u201cHow do you back your car out of your driveway?\u201d we\u2019d probably tell you that &#8211; after checking all the mirrors &#8211; we turn so that we\u2019re facing directly out the back window of the car and then slowly and cautiously begin backing out of the driveway. Now, that\u2019s what we\u2019d <I>tell<\/I> you. Is that the way we <I>really<\/I> back the car out of the driveway, or do we just kind of glance in the rear view mirror while we\u2019re pulling out? Hey, we don\u2019t have to answer questions like that; after all, isn\u2019t this supposed to be a column about <I>scripting<\/I>? <\/P>\n<P>Besides, we never even came <I>close<\/I> to hitting that Chevy. <\/P>\n<P>Believe it or not, we actually had a good reason for bringing up the subject of backing out of the driveway: for some of us, at least, there\u2019s a difference between how we <I>should<\/I> do it and how we actually do it. The same thing is true for checking to see if a user exists in a Windows NT 4 domain (or on a local computer, seeing as how the approach is exactly the same). There\u2019s a way that you <I>should<\/I> make this check, but there\u2019s at least one other way that you <I>can<\/I> make this check. We\u2019ll show you both approaches, then leave it up to you to decide how you should do it. (Hmmm, <B>We Report, You Decide<\/B>. Wonder if anyone else is using that slogan?)<\/P>\n<P>If we were dealing with Active Directory there\u2019d be no question how to do this: we\u2019d just run an ADO (ActiveX Data Objects) search and be done with it. Unfortunately, though, the WinNT provider &#8211; the scripting technology for working with NT 4 domains and with local user accounts &#8211; doesn\u2019t support ADO; thus we <I>can\u2019t<\/I> do a search against those user accounts. Instead, the \u201cofficial\u201d way to check for the existence of a user account in an NT 4 domain (or on a local computer) is to use a script similar to this:<\/P><PRE class=\"codeSample\">Set objDomain = GetObject(&#8220;WinNT:\/\/fabrikam&#8221;)\nobjDomain.Filter = Array(&#8220;User&#8221;)<\/p>\n<p>blnFound = FALSE<\/p>\n<p>For Each objUser in objDomain\n    If objUser.Name = &#8220;kenmyer&#8221; Then\n        blnFound = TRUE\n        Exit For\n    End If \nNext<\/p>\n<p>If blnFound = TRUE Then\n    Wscript.Echo &#8220;The user account exists in the domain.&#8221;\nElse\n    Wscript.Echo &#8220;The user account does not exist in the domain.&#8221;\nEnd If\n<\/PRE>\n<P>The script begins by binding to the fabrikam domain, then creates a <B>Filter<\/B> that limits the returned objects to user accounts. That\u2019s what these two lines of code do:<\/P><PRE class=\"codeSample\">Set objDomain = GetObject(&#8220;WinNT:\/\/fabrikam&#8221;)\nobjDomain.Filter = Array(&#8220;User&#8221;)\n<\/PRE>\n<P>Next we create a variable named blnFound and set the value to False. We\u2019ll use blnFound to keep track of whether or not we can find the user account in question. If we find the account, we\u2019ll change blnFound to True; if we don\u2019t find the account, then blnFound will stay False.<\/P>\n<P>We then set up a For Each loop to loop through the list of user accounts. For each account in the collection we check to see if the <B>Name<\/B> property is equal to kenmyer. If it is, we set the value of blnFound to True, then use the <B>Exit For<\/B> statement to exit the For Each loop. (Having already found the name there\u2019s no reason to cycle through the rest of the collection.) If the Name is <I>not<\/I> equal to kenmyer then we simply loop around and check the next account in the collection.<\/P>\n<P>At the end of the script we check the value of blnFound and then echo back the appropriate message (that is, whether or not the user account exists in the domain). <\/P>\n<P>All that works just fine, but if you have 15-20,000 users in your domain it could take a couple minutes for the script to cycle through all the user accounts. So is there a faster way to check for the existence of a user account in an NT 4 domain or a local computer? Yes, there is:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objUser = GetObject(&#8220;WinNT:\/\/fabrikam\/kenmyer&#8221;)<\/p>\n<p>If Err.Number = 0 Then\n    Wscript.Echo &#8220;The user account exists in the domain.&#8221;\nElseIf Err.Number = -2147022676 Then\n    Wscript.Echo &#8220;The user account does not exist in the domain.&#8221;\nElse\n    Wscript.Echo &#8220;The user account status could not be determined.&#8221;\nEnd If\n<\/PRE>\n<P>This script starts off with the <B>On Error Resume Next<\/B> statement. That\u2019s important, because we aren\u2019t going to retrieve a list of all the user accounts and then methodically check each one to see if the Name equals kenmyer. Instead, we\u2019re going to try to bind directly to the account in question:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;WinNT:\/\/fabrikam\/kenmyer&#8221;)\n<\/PRE>\n<P>If we\u2019re able to bind to the account (that is, if the account exists), then the value of the <B>Err<\/B> object will be equal to 0. We check the value of <B>Err.Number<\/B> and, if it equals 0, we report back that the user account exists in the domain. If Err.Number is equal to -2147022676 we report back that the account does <I>not<\/I> exist in the domain. And if Err.Number is equal to something else we report that the account status could not be determined.<\/P>\n<P>As we said, this approach is faster and requires less code. So then why don\u2019t we \u201cofficially\u201d recommend it? Well, the truth is, a lot of things can potentially go wrong with a script; because of that you hate to rely on the error number as the only way of determining whether an object exists or not. This script should work just fine, but we don\u2019t recommend making a habit of using shortcuts like this. If anyone ever asks you, \u201cDo the Scripting Guys recommend that you rely on error number as the only way to determine whether or not an object exists?\u201d, make sure you tell them \u201cNo, they don\u2019t.\u201d That doesn\u2019t mean we won\u2019t do that from time-to-time, just that we don\u2019t recommend it.<\/P>\n<P>Now, did you say you had a question about cars and about driving? Well, what we Scripting Guys always recommend is &#8211; you still there? Hello? Come on: we know as much as those <A href=\"http:\/\/cartalk.cars.com\/\" target=\"_blank\"><B>Car Talk<\/B><\/A> guys do.<\/P>\n<P>Well, not about <I>cars<\/I>, of course, but \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I check to see if a user exists in an NT 4 domain?&#8212; PA Hey, PA. You know, if you were to ask us, \u201cHow do you back your car out of your driveway?\u201d we\u2019d probably tell you that &#8211; after checking all the mirrors &#8211; we turn so that [&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":[23,24,3,5],"class_list":["post-69013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-local-accounts-and-windows-nt-4-0-accounts","tag-other-directory-services","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I check to see if a user exists in an NT 4 domain?&#8212; PA Hey, PA. You know, if you were to ask us, \u201cHow do you back your car out of your driveway?\u201d we\u2019d probably tell you that &#8211; after checking all the mirrors &#8211; we turn so that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69013","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=69013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69013\/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=69013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}