Skip to content

Automatically delete files listed in a CSV

G26th September 2014

CNo Comments

TPowerShell, Windows

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.



No comments have yet been submitted. Be the first!

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.