Automatically delete files listed in a CSV
A PowerShell script that reads a list of files in a CSV and deletes them from a computer drive automatically.
In order to manage a large number of files on a computer drive, it may be helpful to execute a PowerShell command that will list them in an Excel CSV file.
Through such a process, a large number of files may be identified that can be deleted. Rather than performing this task manually file-by-file, deletion can be automated in PowerShell.
Using my previous code to generate a CSV, the following script will read through the file at c:\files.csv and delete all of the files in that list:
Import-Csv c:\files.csv | Foreach-Object{Remove-Item -LiteralPath $_.FullName -Force}
This script works because the CSV that I’d previously created has a column called “FullName” with the full file paths. This is referenced as $_.FullName
in the code. Capitalisation isn’t important. Nor is the positon of “FullName”.
Deletion of selected image files
I recently published another method for filtering image files in a directory, based on several parameters including pixel dimensions. These were also added to a CSV.
The above code can also be used with the output CSV files from this second process, because the file paths are still listed in a column entitled “Fullname”.
Be careful
None of the files that are deleted are sent to the Recycle Bin. They’re deleted permanently, so exercise extreme caution in using this script.
Ideally, the CSV file should be generated and then reviewed before the deletion script is executed.
Comments
3 responses to “Automatically delete files listed in a CSV”
Hi, I’m getting an error
Remove-Item : Impossibile associare l’argomento al parametro ‘LiteralPath’ perché è null.
In riga:1 car:74
+ … c2.csv” | Foreach-Object{Remove-Item -LiteralPath $_.FullName -Force}
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemC
ommand
translating from italian it means that it is impossible to associate the parameter -LiterlPath because it’s Null.
I used your previous script to obtain the csv list and deleted the rows I wanted to keep.
Could you please help me?
Hey, found the culprit.
The thing is that my directory name contained a comma “,” and therefore the script reading the CSV thought it was a “separator”.
Replaced the “,” with a “_” and voila’!
Thanks for the script.
Hi Adam,
Thanks for the site and scripts.
I’m using your script to export filenames to CSV. I’m then editing the CSV to select a subset of files that I want to delete.
So we have a folder with 850000 files and I’m deleting maybe 50000 at a time. A months worth.
The delete script works. But I get this error over and over.
Remove-Item : Cannot bind argument to parameter ‘LiteralPath’ because it is an empty string.
At line:2 char:62
+ Import-Csv $source | Foreach-Object{Remove-Item -LiteralPath $_.FullName -Force}
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.
Commands.RemoveItemCommand
Only the deleted files seem to have been removed. So I think this might be script reading/erroring on the files from the folder that aren’t in the CSV?