{"id":70463,"date":"2005-02-10T15:34:00","date_gmt":"2005-02-10T15:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/10\/how-can-i-disable-the-guest-account-on-a-computer\/"},"modified":"2005-02-10T15:34:00","modified_gmt":"2005-02-10T15:34:00","slug":"how-can-i-disable-the-guest-account-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-disable-the-guest-account-on-a-computer\/","title":{"rendered":"How Can I Disable the Guest Account on a Computer?"},"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 whether or not the Guest account is enabled on a computer and, if it is, disable it?<BR><BR>&#8212; PR<\/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, PR. At last: a question for which there is a very simple, very straightforward answer. This isn\u2019t to minimize the importance of the task: in general, it\u2019s highly recommended that you disable the Guest account on your computers. It\u2019s just nice to have a question where we don\u2019t have to come up with some crazy workaround.<\/P>\n<P>Let\u2019s start with determining whether or not the Guest account is enabled on a computer. To do that, we simply need to bind to the Guest account and then check the value of the <B>AccountDisabled<\/B> property. How hard is that? Well, it\u2019s no harder than this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\nSet objUser = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Guest&#8221;)<\/p>\n<p>Wscript.Echo &#8220;Guest account disabled: &#8221; &amp; objUser.AccountDisabled\n<\/PRE>\n<P>That\u2019s right, just three lines of code. We assign the name of the computer (<B>atl-ws-01<\/B>) to the variable strComputer, then use ADSI and the WinNT provider to bind to the Guest account on that computer. (<B>Note<\/B>: With the WinNT provider, case is important; don\u2019t type something like <I>winnt<\/I> \u2026 unless, of course, you don\u2019t want a script that actually works). Finally, we echo back the value of the AccountDisabled property. If the Guest account is disabled, then AccountDisabled will be True; if the account is enabled, then AccountDisabled will be False. <\/P>\n<P>See why we liked this question so much?<\/P>\n<P>Of course, you might be thinking, \u201cWell, sure, determining whether the Guest account is enabled is easy, but what about <I>disabling<\/I> that account? How hard is <I>that<\/I> going to be?\u201d Well, we have to be honest with you: this is definitely harder. After all, it takes <I>four<\/I> lines of code rather than just three:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\nSet objUser = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Guest&#8221;)<\/p>\n<p>objUser.AccountDisabled = True\nobjUser.SetInfo\n<\/PRE>\n<P>As you can see, we bind to the Guest account and set the value of AccountDisabled to True. We then call the SetInfo method to actually write this change to the account, disabling it.<\/P>\n<P>That\u2019s it. If you want to first check the value of the account and then disable it, you can simply combine the scripts, like so:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\nSet objUser = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Guest&#8221;)<\/p>\n<p>Wscript.Echo &#8220;Guest account disabled: &#8221; &amp; objUser.AccountDisabled<\/p>\n<p>objUser.AccountDisabled = True\nobjUser.SetInfo\n<\/PRE>\n<P>Note that no error is generated if you try to disable an account that\u2019s already disabled. Therefore, we simply echo the current account status and then go ahead and disable it. If the account is already disabled, hey, no problem; the script will just gracefully terminate.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine whether or not the Guest account is enabled on a computer and, if it is, disable it?&#8212; PR Hey, PR. At last: a question for which there is a very simple, very straightforward answer. This isn\u2019t to minimize the importance of the task: in general, it\u2019s highly recommended [&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,63,5],"class_list":["post-70463","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-security","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine whether or not the Guest account is enabled on a computer and, if it is, disable it?&#8212; PR Hey, PR. At last: a question for which there is a very simple, very straightforward answer. This isn\u2019t to minimize the importance of the task: in general, it\u2019s highly recommended [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70463","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=70463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70463\/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=70463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}