Powershell to delete all files with a certain file extension

Solution 1:

Use del *.<extension> or one of it's aliases (like rm, if you are more used to bash).

So it would be del *.avi to delete all files ending in .avi in the current working directory.

Use del <directory>\*.<extension> to delete files in other directories.


WARNING: Both del *.<extension> and del <directory>\*.<extension> will delete other extensions that match the pattern. These files are not sent to the Recycle Bin.

Example: del *.doc* deletes *.doc, *.docm and *.docx. The del <directory>\*.<extension> works in a similar fashion.

Solution 2:

Assuming the preferred method of opening a Powershell instance in the directory, a generic version would be as follows:

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

For a directory-specific version:

Get-ChildItem -Path 'C:\Users\ramrod\Desktop\Firefly\' *.avi | foreach { Remove-Item -Path $_.FullName }

Add in -Recurse to Get-ChildItem to affect all contained folders.

Example:

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