PowerShell: How to Delete All Files with Specific Extension

You can use the following basic syntax to delete all files with a specific extension in a directory using PowerShell:

Get-ChildItem *.csv | foreach { Remove-Item -Path $_.FullName }

This particular example deletes all files with a .csv file extension in the current directory.

The following example shows how to use this syntax in practice.

Example: How to Delete All Files with Specific Extension Using PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

We can see that there are three files with .txt extensions and three files with .csv extensions.

Suppose that we we would like to delete all of the files with .csv extensions:

  • awesome_data.csv
  • cool_data.csv
  • neat_data.csv

We can use the following syntax to do so:

Get-ChildItem *.csv | foreach { Remove-Item -Path $_.FullName } 

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

We can see that we were able to successfully delete all three files that had a .csv extension in the directory:

  • awesome_data.csv
  • cool_data.csv
  • neat_data.csv

Note that we used the command Get-ChildItem *.csv to specify that we only wanted to delete the files that had .csv as the extension.

All other files with different file extensions in the same directory are left untouched.

Related Tutorials

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

PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard
PowerShell: How to Replace Special Characters in String

Leave a Reply