Powershell – How to restore a Windows administrative hidden share

filespowershell

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

Best Answer

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