This script will scan children of root (currently set too \sitecore\media library
) and return any items that do not have a BlobField specified (i.e. have no media attached).
To use this script:
- Set root by replacing \sitecore\media library
- Optionally recurse through all children and children etc by adding -Recurse to line 6 (i.e. the line that begins with $items =):
$items = Get-ChildItem -Path $sourcePath.Paths.FullPath -Recurse | - Run this script initially to see what is returned in the hash table.
- Replace line 33 (the line that reads $item nested in the middle of Get-MediaItemWithNoBlob) with the following when you want to execute the operation:
Get-MediaItemWithNoBlob | Remove-Item
$sourcePath = Get-Item "/sitecore/media library";
$systemFolderToExclude = "System";
$templateNameToExclude = "FDAMediaFolder";
$nodeTemplateNameToExclude = "Node";
Write-Host "Finding items with no blobs: " $sourcePath.Paths.FullPath;
function Get-MediaItemWithNoBlob {
$items = Get-ChildItem -Path $sourcePath.Paths.FullPath |
Where-Object { ($_.Name -NotMatch $systemFolderToExclude) -and ($_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder) -and ($_.TemplateName -NotMatch $templateNameToExclude) -and ($_.TemplateName -NotMatch $nodeTemplateNameToExclude) }
foreach($item in $items) {
if(!(HasBlob($item))) {
if(!(HasChildren($item))) {
$item
}
}
}
}
function HasBlob($item)
{
$name = $_.Name
$mediaItem = [Sitecore.Data.Items.MediaItem]$item
$blobField = $mediaItem.InnerItem.Fields["blob"]
![string]::IsNullOrEmpty($blobField)
}
function HasChildren($item)
{
$contOfImages = 0
$children = $item.Children
foreach($chItem in $children){
$contOfImages++
}
$contOfImages -gt 0
}
# Setup a hashtable to make a more readable script.
$props = @{
InfoTitle = "Media items without blobs"
InfoDescription = "Lists all media items without blobs. i.e. no media attached"
PageSize = 25
}
# Passing a hashtable to a command is called splatting. Call Show-ListView to produce
# a table with the results.
Get-MediaItemWithNoBlob |
Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
@{Label="Updated"; Expression={$_.__Updated} },
@{Label="Updated by"; Expression={$_."__Updated by"} },
@{Label="Created"; Expression={$_.__Created} },
@{Label="Created by"; Expression={$_."__Created by"} },
@{Label="Path"; Expression={$_.ItemPath} }
Close-Window