Sitecore PowerShell script to find media items that are not in use with a prompt for the folder location
<# | |
.SYNOPSIS | |
Lists all media items that are not linked to other items. Extended from original to allow the user to provide a location to search | |
.NOTES | |
Adapted from the script by Michael West | |
#> | |
function HasReference { | |
param( | |
$Item | |
) | |
$linkDb = [Sitecore.Globals]::LinkDatabase | |
$linkDb.GetReferrerCount($Item) -gt 0 | |
} | |
function Get-MediaItemWithNoReference($location) { | |
$items = Get-ChildItem -Path "master:$($location)" -Recurse | Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder } | |
foreach($item in $items) { | |
if(!(HasReference($item))) { | |
$item | |
} | |
} | |
} | |
$item = Get-Item -Path "master:\media library\" | |
$result = Read-Variable -Parameters ` | |
@{ Name = "item"; Title="Analyse subitems of"; Tooltip="Folder you want to analyse."; Root="/sitecore/media library/"} ` | |
-Title "Find all media items that are not in use" -Width 600 -Height 200 ` | |
-OkButtonName "Proceed" -CancelButtonName "Abort" | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
$items = Get-MediaItemWithNoReference $item.ProviderPath | |
if($items.Count -eq 0) { | |
Show-Alert "There are no unused media items in that location." | |
} else { | |
$props = @{ | |
InfoTitle = $PSScript.Name | |
InfoDescription = "Lists all media items that are not linked from other items with a prompt for the media library location." | |
PageSize = 25 | |
Title = $PSScript.Name | |
} | |
$items | | |
Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} }, | |
@{Label="Path"; Expression={$_.ItemPath} }, | |
@{Label="ID"; Expression={$_.ID} }, | |
@{Label="Updated"; Expression={$_.__Updated} }, | |
@{Label="Updated by"; Expression={$_."__Updated by"} }, | |
@{Label="Created"; Expression={$_.__Created} }, | |
@{Label="Created by"; Expression={$_."__Created by"} } | |
} | |
Close-Window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment