{"id":68303,"date":"2005-12-19T22:28:00","date_gmt":"2005-12-19T22:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/19\/how-can-i-delete-the-c-share-from-a-computer\/"},"modified":"2005-12-19T22:28:00","modified_gmt":"2005-12-19T22:28:00","slug":"how-can-i-delete-the-c-share-from-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-the-c-share-from-a-computer\/","title":{"rendered":"How Can I Delete the C$ Share from 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 delete the C$ share from a computer?<BR><BR>&#8212; RB<\/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, RB. One quick clarification here: we can tell you how to <I>temporarily<\/I> delete the C$ share from a computer. However, the share will automatically be recreated any time the Server service is stopped and then restarted, or any time the computer is rebooted. Why? That\u2019s just the way these so-called \u201cadministrative shares\u201d work. They\u2019re like dandelions or stray cats: once you have them, you can\u2019t <I>really<\/I> get rid of them.<\/P>\n<P>That said, here\u2019s a script that will at least temporarily delete the C$ share:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colShares = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Share Where Name = &#8216;C$'&#8221;)<\/p>\n<p>For Each objShare in colShares\n    objShare.Delete\nNext\n<\/PRE>\n<P>This script starts off by connecting to the WMI service on the local computer. We then use this line of code to retrieve a collection of all the shares that have the <B>Name<\/B> C$:<\/P><PRE class=\"codeSample\">Set colShares = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Share Where Name = &#8216;C$'&#8221;)\n<\/PRE>\n<P>Because share names must be unique on a computer our collection will have &#8211; at most &#8211; just one item in it. Because it <I>is<\/I> a collection, however, we still need to set up a For Each loop to cycle through all the members in the collection. Inside that loop we call the <B>Delete<\/B> method to stop sharing the folder.<\/P>\n<TABLE id=\"EPD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Despite the name, the Delete method doesn\u2019t actually delete anything; it simply stops sharing the folder. The Delete method will not remove the share or any of its contents; it just prevents the data from being shared over the network.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Like we said, this effect is temporary: if the Server service is stopped and restarted or if the computer is rebooted then the C$ share will magically appear. Because of that you might want to put this code in a logon or computer startup script; that way the share will be removed even <I>if<\/I> the computer gets rebooted.<\/P>\n<P>And, yes, you can delete the C$ share (or any other share) on a remote computer; all you have to do is assign the name of that computer to the variable strComputer. For example, here\u2019s a revised script that deletes the C$ share on the remote computer atl-ws-01:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colShares = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Share Where Name = &#8216;C$'&#8221;)<\/p>\n<p>For Each objShare in colShares\n    objShare.Delete\nNext\n<\/PRE>\n<P>Hasta la vista, C$.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete the C$ share from a computer?&#8212; RB Hey, RB. One quick clarification here: we can tell you how to temporarily delete the C$ share from a computer. However, the share will automatically be recreated any time the Server service is stopped and then restarted, or any time the [&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":[3,185,12,5],"class_list":["post-68303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-shared-folders-and-mapped-drives","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I delete the C$ share from a computer?&#8212; RB Hey, RB. One quick clarification here: we can tell you how to temporarily delete the C$ share from a computer. However, the share will automatically be recreated any time the Server service is stopped and then restarted, or any time the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68303","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=68303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68303\/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=68303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}