How to delete files from a list using PowerShell?

In this PowerShell tutorial, we will delete the files picked from a list of files stored in a CSV file. Before you can implement these commands in a PowerShell window, please ensure you have administrator access to Powershell.

The PowerShell instance will need to load with full administrative access.

Basically, the Powershell commands will perform the following operations:

  1. We will check if the file exists.
  2. If the file exists, we will delete the file. Else, we will report that the file does not exist.

You have the option to use the commands in silo or make a PowerShell script based on the actions performed below.

The set of commands or script used to delete files will use the following process flow:

Step 1 – Create a CSV file with list of files to delete

  • We create a CSV file with list of files that need to be checked and deleted. For this to be successful, the first row of the CSV must state the heading. The remaining rows must list full path of the file to be checked for and delete. See the screenshot of the sample CSV file below.
Powershell CSV file with list of files to delete

Step 2 – Load the CSV file

Define a file path variable in PowerShell to define the source location of the CSV file. This step is very important. You will need to load the file in the shell before you can import content from it for checking and deleting.

To load the CSV file for reading, we will use the following Powershell command and load the CSV file:

$csvFilePath = “d:\pwsh tutorial\del1.csv”

$csvFilePatch is a variable that stores the actual file location of the CSV file. This CSV file contains list of files to delete on separate rows.

Now, the file is loaded in memory. We need to import content of the file for preparing a list of files that ought to be deleted.

$fileList = Import-Csv -Path $csvFilePath

So, in this step, we have loaded the file and imported the contents of the file for further processing through a Powershell script or commands involving the for loop.

Step 3 – Check if file exists, and delete the file

The final step is to use the foreach loop in Powershell. It will check all the rows of a CSV file and delete the file (if it exists). The full loop and the Powershell command is given below.

foreach ($file in $fileList) {
    $filePath = $file.FilePath

    # Check if the file exists
    if (Test-Path -Path $filePath) {
        # Delete the file
        Remove-Item -Path $filePath
        Write-Output "Deleted file: $filePath"
    } else {
        Write-Output "File does not exist: $filePath"
    }
}

This should delete the files after reading the list from a CSV file. For your ready reference, the screenshot below shows all the 3 steps performed in a Powershell window.

You can see the script or command output wherein all the 5 files are deleted.

Powershell delete files from a list in CSV file

This completes PowerShell tutorial to delete files listed in a CSV file stored on a local computer.

Rajesh Dhawan

Rajesh Dhawan is a technology professional who loves to write about Cyber-security events and stories, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.