33

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?

2
  • 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
  • 3
    Technically, rm and del are both aliases of the Remove-Item cmdlet. – root Jun 21 '16 at 14:33
19

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.

2
  • 18
    To recursively do this ls *.avi -Recurse | foreach {rm $_} – Kolob Canyon Jan 19 '17 at 0:31
  • 2
    To recursively do this for hidden files ls *.ini -Recurse -Force | foreach {rm -Force $_} – Kirk Aug 28 '20 at 2:59
18

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

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.
2
  • 1
    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
  • 3
    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
0

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 $_}

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

New contributor
Pawan Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
-1

try this

Remove-Item "C:\Users\ramrod\Desktop\*.avi" 
1
  • 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 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.