Skip to content

Instantly share code, notes, and snippets.

@rposbo
Created October 7, 2014 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rposbo/8d55b8f76572145a8889 to your computer and use it in GitHub Desktop.
Save rposbo/8d55b8f76572145a8889 to your computer and use it in GitHub Desktop.
Delete unreferenced images using Powershell
# cd to your project directory
cd "d:\my luverly project\"
# get all the images
$images = Get-ChildItem "d:\my luverly project\img\" -Exclude "*.config"
# for each image..
foreach($img in $images){
$found = ""
# look in css, js, cshtml files for a match
foreach($file in Get-ChildItem -Include "*.css", "*.js","*.cshtml" -Recurse){
# got a match!
if (Select-String -pattern $img.Name -SimpleMatch -Quiet -Path $file.FullName){
$found = $img.Name
}
}
# no match? delete the image
if ($found -ne $img.Name){
Remove-Item $img.FullName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment