Powershell to delete all files with a certain file extension

21

3

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?

user2676140

Posted 2016-06-20T11:59:51.063

Reputation: 1 629

3What prevents you from using rm *.avi? (Yes, that's valid PowerShell syntax because rm is an alias of del. – GiantTree – 2016-06-20T12:08:54.880

2Technically, rm and del are both aliases of the Remove-Item cmdlet. – root – 2016-06-21T14:33:59.420

Answers

14

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.

GiantTree

Posted 2016-06-20T11:59:51.063

Reputation: 828

13To recursively do this ls *.avi -Recurse | foreach {rm $_} – Kolob Canyon – 2017-01-19T00:31:15.990

11

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

Posted 2016-06-20T11:59:51.063

Reputation: 508

1

The Remove-Item commandlet should be all you need. Just pass the root folder where the avi files exist and pass the recurse flag 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 prompt you before deleting each file, which is safer for people less comfortable with PowerShell.

Keith Miller

Posted 2016-06-20T11:59:51.063

Reputation: 1 789

1Not 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 – 2020-01-17T12:11:31.280

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 – 2020-01-17T14:43:12.603

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

guest-vm

Posted 2016-06-20T11:59:51.063

Reputation: 2 984

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.

visionmn2

Posted 2016-06-20T11:59:51.063

Reputation: 1

0

try this

Remove-Item "c:\temp\*.txt" 

Esperento57

Posted 2016-06-20T11:59:51.063

Reputation: 101

1hey 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 – 2019-09-28T09:24:52.333