Link to home
Create AccountLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

How to delete hundreds of shared directories.

I'd like to delete the contents for 4 directories in Server 2008 R2.

These directories and their sub-directories are shared.

Trying to delete gives a warning prompt that they are shared and users will not be able to access them any longer.

There are hundreds of directories to delete.

Is there a way to automate this?

del *.* /F /Q fails.
Avatar of John
Flag of Canada image

One way (not at all long) is to open Explorer, highlight all the files, click on Delete and delete what you can.

You may want User to log off while you do this. This will help inform you on using your script
Avatar of Sheldon Livingston


John... I am trying to avoid having to confirm the deletion of shared folders hundreds of times.  Via Explorer you are prompted for every shared folder.
Go into the folder and delete the files. That is really easy to do and may give you some insight into what cannot be deleted.

My suggestion is a learning exercise to help you with the deletion script.
???  Thanks John.

Anyone else?
Avatar of kevinhsieh
All of the shares are defined in the registry. You can export the registry key, modify it to delete all the shares you don't want anymore, and then restart the lanmanserver server to complete the deleting of the shares. Then you should have no issue deleting the directories.
Go to Computer management and delete all shares you want to clean
After that you will be able to delete all of them if you are computer / domain admin and you inherit all rights from root

User generated image
Thanks Tom... but the folders are all under one share and they are a subset of the folders within the share.
So if there is only one share folder, just go to properties and security, Advance, Owner,

Click Edit and Take ownership, select all subfolders
Click OK, and close all
Go back to same place, then in Permissions click Change Permissions assign all permissions to yourself, then select checkbox

Replace all child object permissions with inheritable permissions from this object
Click OK.
Now you should have ALL permissions to all subfolders and files.,

Now You can delete them all
Find out the name and physical locations on the server of all the shares that you want to remove (just the Root)

on CMD :   "Net Share"
Get the physical URLs

Open in new window

Example of removal of the emails:

gci -Directory -Path F:/ -directory | where{ $_.Name -like "123"} | Remove-Item

Open in new window

"address1","address2","adddress3" |  gci -Directory -Path F:/ -directory | where{ $_.Name -like "123"} | Remove-Item

Open in new window

Where addressN is the physical paths that you got on the net share (just the root folders)

It depends - do you know scripting in VBScript or PowerShell. I would recommend PowerShell but don't know off the top of my head a script.
I would definitely not use the GUI.

Avatar of Sheldon Livingston
Sheldon Livingston
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account