Bulk network folder deletion

by wvc at 2013-04-26 10:30:18

Hi All,
Powershell noob here. What I want to basically do is delete old users H drives using script. I have a CSV file which reads like below:-

Folder Path
and so on

These paths sit on a nas server. I want to read the path from csv and if the folder exist delete it. I have tried couple of this to no avail.

Set-Location “\XXX-nas1\users”
$TargetFolder = Import-Csv H:\folderdeletetest.csv
ForEach ($Folder in $Folders) {

Remove-Item -Path $Folder.Folder -Recurse -Force

I was wonder if i can get some help . That will highly appreciated. Cheers
by DonJ at 2013-04-26 11:18:20
So, you import the folders into $TargetFolder but never do anything with it. Also, $Folders doesn’t contain anything. Finally, headers in a CSV with spaces in them are a pain.

If your CSV is this:


Then you could do this:

New-PSDrive -Root \XXX-nas1\users -PSProvider FileSystem -Name USERS
$folders = Import-CSV H:\folderdeletetest.csv
ForEach ($folder in $folders) {
Remove-Item -Path $folder.folderpath -recurse -force

I mapped a drive rather than changing right to the UNC, mainly because that’s just a habit of mine.

I want to point out, however, that your CSV file contains absolute paths (starting with U:). So you’re going to have to make sure U:\ exists, and in that case there’s no need to create a new drive or to change to a particular location.

You could also do this a lot more easily. Instead of a CSV file, just make a text file.


Make sure U: exists and points to the right place.

Remove-Item -Path (Get-Content h:\folderdeletetest.csv) -recurse -force

The -Path parameter can accept multiple values like that.
by wvc at 2013-04-26 11:32:41
Thanks DonJ. So lets say If i have a text file instead of csv which reads like

I.E no absolute path. How do I compair if the foldername in the text file exist on the network share and then delete

$folders = Import-CSV H:\folderdeletetest.csv
ForEach ($folder in $folders) {
Remove-Item -Path $folder.folderpath -recurse -force
by mjolinor at 2013-04-26 11:36:48
There’s a problem with using the -Recurse switch with Remove-Item.
The Help, even in V3, states:

The Recurse parameter in this cmdlet does not work properly.

Basically it seems to be an problem with enumeration order that causes it to try and remove folders before all the child items in the folder are removed.

I’ve used this as a workaround:

get-childitem <folder path> -Recurse |
select -ExpandProperty Fullname |
sort length -Descending |
foreach { Remove-Item -LiteralPath $_ -Force}

Sorting the FullNames in descending order by length insures that no parent folder is deleted before all the childitems in tha folder have been deleted.

Using the -LiteralPath parameter prevents problems you’ll have any of the files have square brackets in their file names, but forces you to use foreach-object instead of just being able to pipe it straight into Remove-Item.
by DonJ at 2013-04-26 11:37:36
Thanks for that - I actually wasn’t even paying attention to -Recurse ;).
by mjolinor at 2013-04-26 11:47:47
That -Recurse parameter might be a candidate for your “gotcha” list.

I ran into it in V2, and really expected that they would either fix that parameter so it works right, or just remove it in V3, but it’s still the same.
by wvc at 2013-04-26 11:54:22
Thanks mjolinor. Please shed some light on my question above too if you can. Cheers
by mjolinor at 2013-04-26 11:56:46
I’ll try.
What do the first few lines of your .csv file look like?
by wvc at 2013-04-26 12:00:09
Csv looks like below:-

Code :

Set-Location “\xxx-nas1\users”
$TargetFolder = Import-Csv c:\folderdeletetest.csv
ForEach ($folderpath in $TargetFolder) {

Remove-Item -Path $folderpath.Folder -Recurse -Force

Goal is to compair if a folder in csv exist on the network share. If it exist , then delete it. If its simpler to do it via text file without absolute path (i.e foldername only in the text file). I can change it to that.
by mjolinor at 2013-04-26 12:27:30
See if this works for you:

$TargetFolder = Import-Csv c:\folderdeletetest.csv

ForEach ($folderpath in $TargetFolder) {
$DirPath = $FolderPath.FolderPath -replace ‘u:’,’\xxx-nas1’
get-childitem $DirPath -Recurse |
select -ExpandProperty Fullname |
sort length -Descending |
foreach { Remove-Item -LiteralPath $_ -Force
by wvc at 2013-04-26 13:02:35
Thanks Mjolinor,
i just left work, so I will go home and try. Any chance we can print message on screen saying E.g Deleting folder 1 or foldername while the script is running and outputting the deleted folder into a text file. Thanks again for your help. Cheers
by mjolinor at 2013-04-26 13:20:43
Are you just wanting a visual record of what was deleted, or something ready to import for analysis or scripting?

You can add -Verbose to the Remove-Item to display what’s being deleted, and use Start-Transcript to capture everything that’s being written to the console to disk.
by wvc at 2013-04-29 06:25:53
Hi Mjolinor,
Thanks for your help. That script works like a charm. No issues. I want something ready to import for analysis in case a wrong folder is deleted and all. I just need to keep a record of what’s being deleted.