Skip to main content

Get the count and size of the subfolders of media folders and media items for each of the medial folders inside media library

I need to get the count of all the media items and folders inside any media folder in media library. Then export the data to csv I am planning to write a script using SPE. Please help me on the same

For Example

A(Media Folder) - 8MB

A1 (Media Item) - 2MB

A2 (Media Item) - 2 MB

A3 (Media folder) - 4MB

A31 (Media Item) - 2 MB

A32 (Media Item) - 2MB

B(Media Folder) - 3MB

B1 (Media Item) - 1MB

B2 (Media folder) - 2MB

B21 (Media Item) - 1MB

B22 (Media Item) - 1MB

I am using below script found in one of the answer but I want the size of the subfolder also as shown above:

$mediaLibriary = Get-Item "/sitecore/media library"
$mediafoldersTemplates = "Media folder", "Node"

$root = $mediaLibriary.Children
$table = @()

foreach($rItem in $root){
    if($mediafoldersTemplates -contains $rItem.TemplateName){
        $table += ProcessChildren -item $rItem
    }
}

$table  | Format-Table -AutoSize

function ProcessChildren($item)
{
    $contOfFolders = 0
    $contOfImages = 0

    $children = $item.Children

    $result = @()
    $tempTable = @()
    foreach($chItem in $children){
        if($mediafoldersTemplates -contains $chItem.TemplateName)
        {
            $contOfFolders++
            $tempTable += ProcessChildren -item $chItem
        }else{
            $contOfImages++
        }
    }


    $objAverage = New-Object System.Object
    $objAverage | Add-Member -type NoteProperty -Name Name -Value $item.Name
    $objAverage | Add-Member -type NoteProperty -Name FolderCount -Value $contOfFolders
    $objAverage | Add-Member -type NoteProperty -Name ItemCount -Value $contOfImages

    $result += $objAverage
    $result += $tempTable

    return $result
}