0

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 doing
net 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 of
net file
and
net share
in PowerShell

3 Answers 3

2

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

3
  • Cool. 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. Commented May 19, 2009 at 13:58
  • You sure that takes care of making the permissions identical to what server would normally create though? Commented May 19, 2009 at 13:59
  • Good 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.
    – user2278
    Commented May 19, 2009 at 15:46
2

I believe you need to restart the "Server" service.

1
  • +1 for that being the answer both before and after the fact (I just wanted to yank people off the share) Commented May 19, 2009 at 14:00
0

I believe you can go to Control Panel => Administrative Tasks => Computer Management, scroll down to Shared Folders => Shares.

Now, right click => New File Share.

Enter folder to share (i.e. E:), share name as E$, click next, select Administrators have full access, others have no access, and finalize.

Double clicking the new share shows the same message as the other special shares.

However, I'm not 100% this is the same exact share as the default Windows ones because mine keeps disappearing after a restart on just one of the drives while the others are fine.

2
  • I 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. Commented May 19, 2009 at 10:00
  • 1
    Not much better but experimenting may reveal that restarting the responsible service may suffice instead of the reboot. Commented May 19, 2009 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .