Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

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.



2 responses to “Automatically delete files listed in a CSV”

On 13 January 2021, Lorenzo wrote: Hyperlink chain icon

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

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?


    On 14 January 2021, Lorenzo wrote: Hyperlink chain icon

    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.


Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.