Skip to content

Instantly share code, notes, and snippets.

@RyanDurkin
Last active March 16, 2018 11:45
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 RyanDurkin/992aaff199b706a3a7920d956055f529 to your computer and use it in GitHub Desktop.
Save RyanDurkin/992aaff199b706a3a7920d956055f529 to your computer and use it in GitHub Desktop.
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