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

35,052

Solution 1

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

Solution 2

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.

Solution 3

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).

Share:
35,052
Techrocket9
Author by

Techrocket9

I am not dead.

Updated on November 11, 2020

Comments

  • Techrocket9
    Techrocket9 over 3 years

    I know a common characteristic of the file names of a number of unwanted files on my Windows computer. How can I remove all of these files from a given folder or folder hierarchy with a single regular expression PowerShell command?

  • mjolinor
    mjolinor about 10 years
    Select-String is designed for finding information in files. It returns MatchInfo objects that contain information like the file name and line number where the match was found. In this context, all that information is useless and the overhead it adds is wasted. A simple -match operation would be faster, simpler, and just as effective.
  • Bill_Stewart
    Bill_Stewart about 10 years
    I would add that the -name parameter outputs strings only, so you lose all other file metadata (Length, LastWriteTime, etc.). I recommend matching on the Name property instead (using -match as mjolinor suggested).
  • mjolinor
    mjolinor about 10 years
    @Bill_Stewart - I'd recommend using Fullname, especially if you doing a recurse.
  • Bill_Stewart
    Bill_Stewart about 10 years
    @mjolinor - Yes, if the path part of the name is important, of course I agree.
  • TheMadTechnician
    TheMadTechnician about 10 years
    Passing the whole FileInfo object (rather than a string) to the Remove-Item command will remove it even if it is not in your current path. Matching against Name avoids false positive matches from simplistic regex patterns accidently matching a parent folder.
  • mjolinor
    mjolinor about 10 years
    True, but that seems like a crutch to protect yourself from your own regexes.
  • TheMadTechnician
    TheMadTechnician about 10 years
    If you're looking to match a pattern in a file/folder name, then there really shouldn't be any need to have the full path. The OP said to regex match a pattern in the file name, referencing the whole path means you have to spend more time on your regex pattern and make it more exact. I wouldn't call referencing Name a crutch so much as only working with what's needed for the desired task.
  • dfernan
    dfernan over 8 years
    Note: Get-ChildItem does not retrieve hidden files by default. This behavior can be changed with -Force or -Attributes: technet.microsoft.com/en-us/library/hh847897.aspx