51

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
  • 64,768
  • 7
  • 111
  • 168
user2676140
  • 2,075
  • 7
  • 30
  • 47
  • 3
    What prevents you from using `rm *.avi`? (Yes, that's valid PowerShell syntax because `rm` is an alias of `del`. – GiantTree Jun 20 '16 at 12:08
  • 4
    Technically, `rm` and `del` are both aliases of the `Remove-Item` cmdlet. – root Jun 21 '16 at 14:33

8 Answers8

36

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 }
David Metcalfe
  • 775
  • 7
  • 18
32

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
  • 8,704
  • 1
  • 15
  • 28
  • 2
    Not sure why this answer got a down-vote. The `-WhatIf` switch is unnecessary. I submitted an edit to this answer. +1 The `Remove-Item` commandlet is all you need, and runs faster than the currently accepted answer. – Greg Burghardt Jan 17 '20 at 12:11
  • 5
    It's a good idea to use `-whatif` to verify you will remove only the files you want to remove. And especially when offering up a deletion command to those of unknown technical expertise. Best to keep the training wheels on. – Keith Miller Jan 17 '20 at 14:43
29

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,048
  • 1
  • 8
  • 15
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,359
  • 17
  • 27
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

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
  • 3
-1

try this

Remove-Item "C:\Users\ramrod\Desktop\*.avi" 
Esperento57
  • 109
  • 4
  • 2
    hey there, this doesn't seem to remove avi files, and it doesn't seem to do it from the folder in question, either. Could you improve on this answer or explain how it might help in this situation ? – Sirex Sep 28 '19 at 09:24