55

I want to scan a directory (there are no subs) and delete any file with an extension of .avi I tried this syntax, but no errors are thrown, and no files are deleted. This is the code I tried

get-childitem 'C:\Users\ramrod\Desktop\Firefly' -include *.avi | foreach ($_) {remove-item $_.fullname}

What should be altered in order to delete all the .avi files from the folder?

Hennes
  • 65,804
  • 7
  • 115
  • 169
user2676140
  • 2,135
  • 8
  • 33
  • 50

8 Answers8

40

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 }
38

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.
Keith Miller
  • 10,694
  • 1
  • 20
  • 35
30

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.

GiantTree
  • 1,098
1

Suppose there are several text files in current directory.
dir * -include *.txt & dir *.txt work as expected but dir -include *.txt gives nothing. The reason is well explained on Stack Overflow.

Corrected command:
dir 'C:\Users\ramrod\Desktop\Firefly\*.avi' | foreach {del $_}

guest
  • 3,419
0

This finally did it for me - tried all above and nada.

get-childitem "<drive>:\<folder>" -recurse -force -include *.<ext> | remove-item -force

sure two -force is not required but that is what is working so that is what I am sticking with.

0

try this

Remove-Item "C:\Users\ramrod\Desktop\*.avi" 
0

del *.raw, *.log, *.op.raw // delete desired type of files. It deletes all files with .raw, .log, .op.raw extension in pwd

ls *.raw, *.log, *.op.raw // see desired type of files

0
$patho = $env:USERPROFILE + '\Downloads\'
foreach($due in gci $patho -name "*.avi" )
{
$monk = $patho + $due
remove-item -path $monk
}

Already answered... just elaborating...using these sorts of codes: Can cause a user / you to accidently permanently delete your own folders by mistake. Quite fundamentally, avoid using remove-item, unless you are certain of recovery or process. A simple dash '' being missed and '*' combined can cause quite the heartache. What you should do to delete sets: Treat as if networking.

  1. Count the number of items you going through, directories or not.

  2. Consider system items associated with counting list.

  3. Consider the count sets as safe to go through or not. (i.e. are you trying to delete multiple items or multiple directories? Probably not the latter) Cancel if multiple directories are in the path of deletion.

  4. Use Move-Item as much as you can, and -whatif or -confirm parameters instead to be sure you don't delete your own user profile.

  5. I always end up deleting my profile somehow anyways!

Dana M
  • 11