PowerShell: How to Use Remove-Item with Multiple Files


You can use the Remove-Item cmdlet in PowerShell to delete specific folders and files.

Often you may want to use the Remove-Item cmdlet to delete multiple specific files at once.

You can use the following methods to do so:

Method 1: Use Remove-Item to Delete Multiple Files by Name

Remove-Item .\data1\coaches.txt, .\data1\players.csv -Force

This particular example will delete the files named coaches.txt and players.csv.

Note that we simply typed the path to the specific file names that we wanted to delete, separated by commas. You can use similar syntax to delete as many specific files as you would like.

Method 2: Use Remove-Item to Delete Multiple Files by Extension

Remove-Item .\data1\* -include *.csv -Force

This particular example will delete all files in the subfolder named data1 that have an extension of .csv.

The following examples show how to use each of these methods in practice with a folder named data1 that contains five total files:

Example 1: Use Remove-Item to Delete Multiple Files by Name

Suppose that we would like to delete the files named coaches.txt and players.csv.

We can use the Remove-Item cmdlet with the following syntax to do so:

Remove-Item .\data1\coaches.txt, .\data1\players.csv -Force

The following screenshot shows how to use this syntax in practice:

PowerShell Remove-Item multiple files

Notice that when we use the ls command again to view all files in the data1 folder that the coaches.txt and players.csv files have been deleted.

Example 2: Use Remove-Item to Delete Multiple Files by Extension

Suppose that we would like to delete all files in the data1 folder that contain .csv as the file extension.

We can use the Remove-Item cmdlet with the -include operator to do so:

Remove-Item .\data1\* -include *.csv -Force

The following screenshot shows how to use this syntax in practice:

PowerShell Remove-Item multiple files based on extension

Notice that when we use the ls command again to view all files in the data1 folder that all files with a .csv extension have been deleted.

Note: You can find the complete documentation for the Remove-Item cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Delete File if it Exists
PowerShell: How to Delete All Files with Specific Extension
PowerShell: How to Delete Files Matching a Certain Pattern

Leave a Reply