I have a list of filenames in a text file like this:
f1.txt
f2
f3.jpg
How do I delete everything else from a folder except these files in Powershell?
Pseudo-code:
Data:
-- begin exclusions.txt --
a.txt
b.txt
c.txt
-- end --
Code:
# read all exclusions into a string array
$exclusions = Get-Content .\exclusions.txt
dir -rec *.* | Where-Object {
$exclusions -notcontains $_.name } | `
Remove-Item -WhatIf
Remove the -WhatIf
switch if you are happy with your results. -WhatIf
shows you what it would do (i.e. it will not delete)
-Oisin