Deleting Files from a List

by Xasthur at 2012-10-28 18:19:33

Fairly new to PowerShell, so bear with me here for a moment. I’ve got a folder with about 10,000 items in it. They’re all from a single archive so they’re all organized under the same naming scheme. The standard Windows 7 search doesn’t seem to have the ability to parse file names whilst recognizing brackets (), so I figured I’d use the PowerShell filter instead. Each file contains a letter in its name which signifies its country of origin, e.g. (E) = Europe. So, after changing to the directory, the following command is the one I used to generate my desired list and store it in a variable]$rsl = get-childitem -filter "(E)" -recurse[/powershell]Now I have all the names of the files I need to get rid of, but I’m not actually sure of how to delete them. My initial thought would be to do something along the lines of "$rsl | ForEach-Object" and using $_ to hit every filename in the list, but I don’t know how to format it so that I could remove-item all of the files from the hard disk. Hopefully that’s clear enough, if not I can try to give some more detail.

Thanks,
-X
by Klaas at 2012-10-29 01:45:05
You can skip the variable assignment, and the foreach. The golden path in Powershell is to send objects to a pipeline:
Get-ChildItem -filter "(E)" -recurse | Remove-Item -Whatif

You’ll get a list of files that would be deleted. If that pleases you, lose the -Whatif switch.
by Xasthur at 2012-10-29 13:09:30
Yup, that worked. Guess I was just overthinking it a bit, but I also didn’t know about the -Whatif switch which is pretty useful. Thanks!

-X