I'm trying to remove all the images which are not listed in the CSV file. Data comes to CSV in the format described below.
Problem is that script is going to delete every file in the images folder. I only want to delete images that do not contain SKU (example "7-5468-XXX7") from the CSV file. What to do here?
Trying to use this:
$filestokeep = Import-Csv "C:\Users\example\filestokeep.csv"
$files = Get-ChildItem "C:\Users\example\images\"
$files |
Where-Object Name -notin $filestokeep.sku |
Remove-Item -WhatIf
filestokeep.csv looks like:
"sku"
"1-123-X"
"2-123-XXXXX"
"3-562-XXXX"
"4-215-XXXXXX"
"5-56482-XX-X"
"6-45688"
"7-5468-XXX"
"856-54648-X"
C:\Users\example\images\ containing files such as:
1-123-X.jpg
2-123-XXXXX.jpg
1-123-X_1.jpg
1-123-X_2.jpg
7-5468-XXX7.jpg
7-5468-XXX7_1.jpg
7-5468-XXX7_2.jpg
6-45688.jpg
Images folder should look like this after running:
1-123-X.jpg
2-123-XXXXX.jpg
1-123-X_1.jpg
1-123-X_2.jpg
6-45688.jpg
Link to original code that I am trying to implement: https://www.reddit.com/r/PowerShell/comments/8f2z0l/remove_files_not_in_csv/