Often you may want to use PowerShell to delete all files in a folder except for specific ones.
You can use the following syntax to do so:
Get-ChildItem -Exclude @("*regions*", "*teams*") | Remove-Item -Force
This particular example deletes all files in the current folder except for the ones that contain the pattern “regions” or “teams” in the filename.
Here is what the various commands do:
- Get-ChildItem retrieves all files in the current folder
- -Exclude specifies patterns not to include when retrieving the files
- Remove-Item deletes the files that have been retrieved
- -Force specifies that hidden files should also be deleted
The following example shows how to use this syntax in practice.
Example: Delete All Files Except Specific Ones Using PowerShell
First, we can use the Get-ChildItem cmdlet to view all files in our current folder:

Suppose that we would like to delete all files in this folder except for the ones that contain the pattern “regions” or “teams” in the filenames.
We can use the following syntax to do so:
Get-ChildItem -Exclude @("*regions*", "*teams*") | Remove-Item -Force
The following screenshot shows how to use this syntax in practice:

Notice that when we use the Get-ChildItem cmdlet again to view the files in the current directory we can see that all files have been deleted except for the two files that contained “regions” or “teams” in the filename.
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 Files Older Than X Days
PowerShell: How to Delete Files Matching a Certain Pattern
PowerShell: How to Rename File Extension of Multiple Files
