Home Gitesh Portfolio Blog About Me Gallery Contact

Sitecore: Powershell query to create a report of large media files

With this powershell command you will be to find large media sizes. Below query will find all media items that are above 5MB.

 

Import-Function -Name ConvertTo-Xlsx

Function AddToList{
Param ($Item, $List, [ref]$Match)
$info = [PSCustomObject]@{
                        "ID"=$Item.ID
						"Path"=$Item.Paths.FullPath
                        "Template"=$Item.TemplateName
						"Size"=$Item.Fields["Size"].Value
						"SizeInMB"=($Item.Fields["Size"].Value)/1000000
                    }
                    [void]$List.Add($info)
					Write-Host ($Item.Paths.FullPath)
					$Match.Value++
}

$path = "master:/media library"
$size = 500000
$list = [System.Collections.ArrayList]@()
$mediaItemContainer = Get-Item $path
$itemsToProcess = $mediaItemContainer.Axes.GetDescendants() | Where-Object { [int]$_.Fields["Size"].Value -gt $size } | Initialize-Item
if($itemsToProcess -ne $null) {
	[int]$match = 0;
    $itemsToProcess | ForEach-Object {
      &AddToList $_ $list ([ref]$match)
    }
	Write-Host ("--- $match media files found. ---")
}
[byte[]]$outobject =  $list | ConvertTo-Xlsx
Out-Download -Name "LargeMediaFilesReport.xlsx" -InputObject $outobject

Posted: 27/04/2022 5:28:19 p.m. by Gitesh Shah | with 0 comments