How do I restore a Windows administrative hidden share
Solution 1
In PowerShell:
# Delete the share - get a WMI instance pointing to C$
# You can specify a remote machine in the moniker, if you want
$share = [WMI]"root\cimv2:Win32_Share.Name='C$'"
$deleteReturnCode = $share.Delete()
# check return code here - 0 is success
# Create the share - use the Win32_Share class.
$shareClass = [WMICLASS]'root\cimv2:Win32_Share'
# parameters are: path, share name, share type - 0 = disk
$createReturnCode = $shareClass.Create('C:\', 'C$', 0)
# check return code here - 0 is success
Obviously you can get instances of Win32_Share and delete them, etc. if you need to. See Win32_Share documentation for error code explanations
Your other option is
net stop server & net start server
but that's a bit heavy as it will obviously disconnect everyone attached to the server
Solution 2
I believe you need to restart the "Server" service.
Related videos on Youtube
Adam M-W
I minimise the biggest liability in any system: Code, mostly by talking to customers so we achieve what they mean. I've been working hard on only writing it when in the Red and keeping it composable since 2006, but I have a few decades left to perfect the art... Mail: my first name at my second name dot com Work: Jet, ProductFitter, InishTech linkedin / +rbartelink / facebook
Updated on September 17, 2022Comments
-
Adam M-W over 1 year
As part of trying to release some files locked by a remote user over an automatic Windows hidden share (which I used to do via the Server control panel in NT4), I decided to skip doing a bulk
net file /c
and did the nuclear option of doingnet share c$ /delete
.Aside from rebooting, what's the correct procedure to restore the share (preferably via the command line) ?
(The net is full of articles explaining that
net share admin$
works implicitly and that there is a registry setting and that you reboot).
Bonus points for linking to a nice explanation of the best practice equivalents ofnet file
andnet share
in PowerShell -
Adam M-W almost 15 yearsI was really hoping for a command line way of getting there (and have retrospectively edited my question to include that), and having it done as Windows would do is important. (The other articles I alluded to say that "net share ADMIN$" doesnt need any extra info and will apply the correct permissions etc. Sadly all the MS KB articles want to talk about rebooting in 2009.
-
Jason Stangroome almost 15 yearsNot much better but experimenting may reveal that restarting the responsible service may suffice instead of the reboot.
-
Adam M-W almost 15 yearsCool. I has hoping there were cmdlets though :P (even pscx ones?) Your restarting server commandline would have been perfect before I removed the share (I wanted to kill the remote locks on files). I guess there are Win32_File stuff representing file locks. But thanks, that's great stuff.
-
Adam M-W almost 15 yearsYou sure that takes care of making the permissions identical to what server would normally create though?
-
Adam M-W almost 15 years+1 for that being the answer both before and after the fact (I just wanted to yank people off the share)
-
user2278 almost 15 yearsGood point - you should probably use the full version of Win32_Share.Create() which takes a Win32_SecurityDescriptor. Getting hold of that from the original share is tricky - you need to use an ASSOCIATORS OF query to get a Win32_SecuritySettingLogicalShareSecuritySetting object and then call GetSecurityDescriptor on it. Or else you can do that once, save the SDDL string and use Win32_SecurityDescriptorHelper.SDDLToWin32SD() , which is a bit easier. Still too long to put in this code sample, through.