How can one delete files in a folder matching a regular expression using PowerShell? How can one delete files in a folder matching a regular expression using PowerShell? powershell powershell

How can one delete files in a folder matching a regular expression using PowerShell?


You can pipe a Get-ChildItem command through a Where-Object filter that accepts a RegEx pattern, and then pipe that into Remove-Item. I think that will get you a faster, and better result than using Select-String. With a command like:

Get-ChildItem $Path | Where{$_.Name -Match "<RegEx Pattern>"} | Remove-Item

The Name attribute will only match the name of the file or folder, along with a file's extension. It will not match against other things along the path. This will pass a FileInfo object down the pipe, which Remove-Item takes as piped input and will remove the files in question.

If you want to include sub folders of your path you would add the -Recurse switch to your Get-ChildItem command, and it would look like this:

Get-ChildItem $Path -Recurse | Where{$_.Name -Match "<RegEx Pattern>"} | Remove-Item

If you only want to delete files you can specify that in the Where statement by looking at the FileInfo object's PSIsContainer property and inverting it by prefixing the object with an exclamation point like such:

Get-ChildItem $Path -Recurse | Where{$_.Name -Match "<RegEx Pattern>" -and !$_.PSIsContainer} | Remove-Item


You can use the command,

ls -name | select-string -pattern ".*\(\d+\).*" | %{rm $_}

Where the content of the quotation marks is your regular expression. The regex in this example searches for files that have (#) in the file name, where # is any nonnegative integer. This is useful for deleting duplicates in a folder where the same set of files have been dumped many times, such as by a music manager.

If you add a -r after the -name

ls -name -r | select-string -pattern ".*\(\d+\).*" | %{rm $_}

it will recurse through subfolders and delete matching files in all subfolders.

The structure of the command is as follows:

  • ls is an alias for the powershell command get-childitem. It lists all elements in the current folder. The -name argument specifies that only the names are to be produced; I don't want other information like file size.
  • select-string is mostly equivalent to UNIX grep, where it matches a pattern (regex) to a bunch of line-separated strings. The -pattern parameter sets the cmdlet up to take a regex.
  • %{rm $} is a foreach loop. It is saying, "for each line piped into me (from select-string in this case)", do the following action, where $ is the given line. In this case we are rm-ing the item, where rm is an alias for Remove-Item.


I'd use this:

(Get-ChildItem -Path $Path | Select -ExpandProperty Fullname) -match <regex> | Remove-Item(Get-ChildItem -Path $Path -Recurse | Select -ExpandProperty Fullname) -match <regex> | Remove-Item

or if you've got V3 or higher so you have automatic member enumeration:

 (Get-ChildItem -Path $Path).Fullname -match <regex> | Remove-Item (Get-ChildItem -Path $Path -Recurse).Fullname -match <regex> | Remove-Item

Name is only going to work if your current working directory is the directory you're deleting files from, and all of the files are in that directory (a recurse may find more than one file with the same name, but different paths).