Powershell to delete all files with a certain file extension

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.


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 }

The Remove-Item Cmdlet should be all you need. Just pass the root folder where the avi files exist and pass the -Recurse parameter along with a filter:

Remove-Item 'C:\Users\ramrod\Desktop\Firefly\*' -Recurse -Include *.avi

This should execute faster than finding the files via Get-ChildItem and invoking Remove-Item for each one.

  • Adding the -WhatIf switch will list the items that would be deleted but the actual deletion is not performed.
  • Adding the -Confirm switch will prompt you before deleting each file, which is safer for people less comfortable with PowerShell.