How to delete files using a search pattern in Windows

How to delete files using a search pattern in Windows

So you have a folder with multiple files inside, and these files follow a naming pattern or maybe you want to delete all files with a certain extension.

There are two ways to do this task, we will cover the two ways to do this task.

Using PowerShell

You can chain a Get-ChildItem command through a Where-Object filter that accepts a RegEX pattern, and then chain the Remove-Item command to remove.

Get-ChildItem $ Path | Donde {$ _.Nombre -Match "RegEx"} | Remover el artículo

The name attribute will only match the name of the file or folder, along with the file extension. It will not correspond to other things in the process. This will pass a FileInfo object through the channel, which Remove-It takes as input to the channel and will remove the files in question.

If you want to include subfolders for the search, we must add the -Recurse option to the Get-ChildItem command:

Get-ChildItem $ Path -Recurse | Donde {$ _.Nombre -Match "RegEx"} | Remover el artículo

If you only want to exclude files, you can specify this in the Where statement, looking at the PSIsContainer property of the FileInfo object:

Get-ChildItem $ Path -Recurse | Donde {$ _.Nombre -Match "RegEx" -and! $ _. PSIsContainer} | Remover el artículo

Using Explorer

For users who are not in the habit of using the command line, there is a possibility to filter using Windows Explorer search and select all filtered files for deletion. This way is not the most efficient and it can take a bit more work, since the filter sometimes returns elements that we don't necessarily want to eliminate, in the following example we see the filter by the extension .png. So if you are going to use this method, be sure to check before selecting all and delete, if all the files listed are actually the ones you want to delete.

Date update on 2020-11-10. Date published on 2020-11-10. Category: Computer class Author: Oscar olg Fuente: marquesfernande