{"id":70723,"date":"2005-01-05T14:40:00","date_gmt":"2005-01-05T14:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/05\/how-can-i-prompt-a-user-to-delete-or-not-delete-a-computer-account\/"},"modified":"2005-01-05T14:40:00","modified_gmt":"2005-01-05T14:40:00","slug":"how-can-i-prompt-a-user-to-delete-or-not-delete-a-computer-account","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-prompt-a-user-to-delete-or-not-delete-a-computer-account\/","title":{"rendered":"How Can I Prompt a User to Delete (or Not Delete) a Computer Account?"},"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 delete a computer account from Active Directory, but provide a Yes\/No prompt to the user before the account is actually deleted?<BR><BR>&#8212; NM<\/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, NM. Let\u2019s start by showing you how to delete a computer account from Active Directory. But don\u2019t worry; that won\u2019t take long. In fact, it only takes two lines of code:<\/P><PRE class=\"codeSample\">Set objComputer = GetObject _\n    (&#8220;LDAP:\/\/CN=atl-ws-01, CN=Computers, DC=fabrikam, DC=com&#8221;)\nobjComputer.DeleteObject (0)\n<\/PRE>\n<P>As you can see, we bind directly to the account we want to delete (this one happens to be a workstation named atl-ws-01, which can be found in the Computers container in fabrikam.com). After that we call the DeleteObject method, which immediately deletes the account from Active Directory. (Incidentally, the<B> (0)<\/B> parameter is required, even though the only allowed value is 0. Leave it out and the script will fail.)<\/P>\n<P>As we noted, the moment you call the DeleteObject method that object is deleted from Active Directory. So how can we prompt the user with an \u201cAre you sure you want to delete this account?\u201d prompt? Well, what we need to do is bind to the account, issue the prompt, and <I>only then<\/I> delete the account (assuming, of course, that the user answers <B>Yes<\/B>). Here\u2019s a sample script that does just that:<\/P><PRE class=\"codeSample\">Set objComputer = GetObject _\n    (&#8220;LDAP:\/\/CN=atl-ws-01, CN=Computers, DC=fabrikam, DC=com&#8221;)\nstrComputer = objComputer.CN<\/p>\n<p>intReturn = Msgbox(&#8220;Are you sure you want to delete this computer account?&#8221;, _\n    vbYesNo, &#8220;Delete &#8221; &amp; strComputer)<\/p>\n<p>If intReturn = vbYes Then\n    objComputer.DeleteObject (0)\nEnd If\n<\/PRE>\n<P>Here\u2019s how the script works. We bind to the computer account, and then retrieve the CN (Common Name) for that account. Note that we don\u2019t really have to grab the CN; we just do that so we can include the computer name in our prompt. We then use VBScript\u2019s Msgbox method to display a message box that includes: 1) a <B>Yes<\/B> button and a <B>No<\/B> button (that\u2019s what the <B>vbYesNo<\/B> constant does for us); 2) displays the message <B>Are you sure you want to delete this computer account?<\/B>; and, 3) just for the heck of it, sets the title of the message box to <B>Delete atl-ws-01<\/B> (the word <B>Delete<\/B> followed by the CN of the computer).<\/P>\n<P>The message box will stay on screen until the user clicks one of the two buttons (Yes or No); as soon as a button is clicked, a value corresponding to that button is assigned to the variable <B>intReturn<\/B>. We then check the value of intReturn. If intReturn is equal to the VBScript constant <B>vbYes<\/B>, then the Yes button was clicked; in that case, we go ahead and delete the computer account. If intReturn is equal to anything else (the only other possibility would be <B>vbNo<\/B>, meaning the No button was clicked) we don\u2019t do anything at all, the script ends, and the account is <I>not<\/I> deleted.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete a computer account from Active Directory, but provide a Yes\/No prompt to the user before the account is actually deleted?&#8212; NM Hey, NM. Let\u2019s start by showing you how to delete a computer account from Active Directory. But don\u2019t worry; that won\u2019t take long. In fact, it 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,46,3,5],"class_list":["post-70723","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I delete a computer account from Active Directory, but provide a Yes\/No prompt to the user before the account is actually deleted?&#8212; NM Hey, NM. Let\u2019s start by showing you how to delete a computer account from Active Directory. But don\u2019t worry; that won\u2019t take long. In fact, it only [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70723","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=70723"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70723\/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=70723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}