# encoding: ascii
# api: powershell
# title: Remove-DeadITunesTracks
# description: This script will go through your ITunes library and check the paths for each of the Tracks. If it doesn’t find a file at the specified location it will remove that track from your ITunes Library.
# version: 0.1
# author: Mark Schill
# license: CC0
# x-poshcode-id: 4486
# x-archived: 2015-05-06T17:07:07
# x-published: 2015-09-24T04:36:00
#
#
$ITunes = New-Object -ComObject iTunes.Application
# The count is dynamic when you call this so you can't use it in the loop
# as you're deleting things or else Write-Progress throws errors at the end
$ItemCount = $ITunes.LibraryPlaylist.Tracks.Count
$toDelete = @()
1..$ITunes.LibraryPlaylist.Tracks.Count | % {
# Can't delete reliably in this loop since item numbers
# get changed as you delete items causing items to be skipped
$CurrentTrack = $ITunes.LibraryPlaylist.Tracks.Item($_)
# File Track ??
if ( $CurrentTrack.Kind -eq 1 )
{
if ( ! ([System.IO.File]::Exists($CurrentTrack.Location)) )
{
Write-Host "$($CurrentTrack.Artist) - $($CurrentTrack.Name) will be deleted."
$toDelete += New-Object -TypeName PSObject -Property (@{
Name = $CurrentTrack.Name
Artist = $CurrentTrack.Artist
HighID = $ITunes.ITObjectPersistentIDHigh($CurrentTrack)
LowID = $ITunes.ITObjectPersistentIDLow($CurrentTrack)
Path = $CurrentTrack.Location
})
}
}
Write-Progress -activity "Collecting Dead Tracks" -status "$($CurrentTrack.Artist) - $($CurrentTrack.Name)" -percentComplete ( $_/$ItemCount*100)
}
$i = 0
$toDelete | % {
Write-Progress -activity "Deleting Dead Tracks" -status "$($_.Artist) - $($_.Name)" -percentComplete (($i++)/$toDelete.Count*100)
$ITunes.LibraryPlaylist.Tracks.ItemByPersistentID($_.HighID,$_.LowID).Delete()
}