13

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?

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

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.

  • 9
    To recursively do this ls *.avi -Recurse | foreach {rm $_} – Kolob Canyon Jan 19 '17 at 0:31
5

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

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.