Hey, Scripting Guy! How can I delete the C$ share from a computer?
— 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 computer is rebooted. Why? That’s just the way these so-called “administrative shares” work. They’re like dandelions or stray cats: once you have them, you can’t really get rid of them.
That said, here’s a script that will at least temporarily delete the C$ share:
strComputer = “.”Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colShares = objWMIService.ExecQuery _ (“Select * from Win32_Share Where Name = ‘C$'”)
For Each objShare in colShares objShare.Delete Next
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 Name C$:
Set colShares = objWMIService.ExecQuery _
(“Select * from Win32_Share Where Name = ‘C$'”)
Because share names must be unique on a computer our collection will have – at most – just one item in it. Because it is 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 Delete method to stop sharing the folder.
|
Note. Despite the name, the Delete method doesn’t 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. |
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 if the computer gets rebooted.
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’s a revised script that deletes the C$ share on the remote computer atl-ws-01:
strComputer = “.”Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colShares = objWMIService.ExecQuery _ (“Select * from Win32_Share Where Name = ‘C$'”)
For Each objShare in colShares objShare.Delete Next
Hasta la vista, C$.
0 comments