Created
May 30, 2025 19:07
-
-
Save MartinMiles/02d05d6ad1175398d731389f749229ad to your computer and use it in GitHub Desktop.
Shows all the media sorted by the size, descending, from the largest to the smallest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You are a Sitecore PowerShell Extensions expert. Please write a complete PowerShell script, ready to paste into Sitecore PowerShell ISE, that: | |
1. Ensures the master: PSDrive is mounted (if not, mount it against the master database). | |
2. Caches the master database object via [Sitecore.Configuration.Factory]::GetDatabase("master"). | |
3. Recursively enumerates all items under “master:\media library”. | |
4. Filters to only those items whose built-in “Size” field is present and non-empty. | |
5. For each such item: | |
- Looks it up by its ID in the master database ($db.GetItem($_.ID)). | |
- Parses the “Size” field (a text string) into a long integer (bytes). | |
- Casts the item to a Sitecore.Data.Items.MediaItem so you can access its Name and ID. | |
- Reads the item’s real Sitecore path via dbItem.Paths.FullPath. | |
- Constructs a [PSCustomObject] with properties: | |
- Name (mediaItem.Name) | |
- Size (bytes) (parsed long) | |
- ID (mediaItem.ID.ToString()) | |
- Path (dbItem.Paths.FullPath) | |
6. Sorts the resulting collection by Size (bytes) descending (largest first). | |
7. Renders the output as a table with exactly these columns in order: Name, Size (bytes), ID, Path, using Format-Table … -AutoSize. | |
Include every line of code—no ellipses—and make sure it runs without errors on a standard Sitecore CM instance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# It will show all the media sorted by the size, descending, from the largest to the smallest | |
# 1) Mount master: PSDrive if it’s not present | |
if (-not (Get-PSDrive -Name master -ErrorAction SilentlyContinue)) { | |
New-PSDrive -Name master -PSProvider Sitecore -Root "\" -Database master | Out-Null | |
} | |
# 2) Cache the master database object | |
$database = [Sitecore.Configuration.Factory]::GetDatabase("master") | |
# 3) Walk the media tree, filter on the built-in Size field, then look up each by ID | |
$results = Get-ChildItem -Path "master:\media library" -Recurse | | |
Where-Object { | |
# only media items whose Size field is non-empty | |
$_.Fields["Size"] -and ($_.Fields["Size"].Value.Trim() -ne "") | |
} | | |
ForEach-Object { | |
# Lookup the real Sitecore item by its ID | |
$dbItem = $database.GetItem($_.ID) | |
if (-not $dbItem) { return } | |
# Parse the Size (stored as text) into a number | |
$size = 0 | |
[long]::TryParse($dbItem["Size"], [ref]$size) | Out-Null | |
# Emit the properties you asked for | |
[PSCustomObject]@{ | |
Name = $dbItem.Name | |
'Size (bytes)' = $size | |
ID = $dbItem.ID.ToString() | |
Path = $dbItem.Paths.FullPath | |
} | |
} | |
# 4) Sort descending by Size and display Name, Size, ID, Path | |
$results | | |
Sort-Object -Property 'Size (bytes)' -Descending | | |
Format-Table Name, 'Size (bytes)', ID, Path -AutoSize |
Author
MartinMiles
commented
May 30, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment