The following PowerShell script searches the folder specified by $folder and filters files using $extension. It displays each matching filename and deletes the file.
PowerShell10 lines
1
2
3
4
5
6
7
8
9
10
$folder="C:\Users"$extension="cmd"if(!($extension)){$filter="*"}Else{$filter=foreach($itemin$extension.split(",")){$item.insert(0,"*.")}}if(test-path$folder){foreach($fileinget-childitem"$folder\*"-recurse-force-include$filter){write-host"Deleting file: $($file.fullname)"remove-item-literalPath$file.fullname-force}#end foreach}#end if
Test the script before using it in production or distributing it.