Here is the script (without logging)
param(
[string]$DeviceID
)
$DeviceID = $DeviceID +’’
$allFolders = Get-ChildItem -Literalpath $DeviceID -Directory -Force
#Create array to store folder objects found with size info.
[System.Collections.ArrayList]$folderList = @()
#Go through each folder in the base path.
ForEach ($folder in $allFolders) {
#Clear out the variables used in the loop.
$fullPath = $null
$folderObject = $null
$folderSize = $null
$folderSizeInMB = $null
$folderSizeInGB = $null
$folderBaseName = $null
#Store the full path to the folder and its name in separate variables
$fullPath = $folder.FullName
$folderBaseName = $folder.BaseName
$folderLastWrite = $folder.LastWriteTime
#Get timeago
$timeago = ((Get-Date) - $folderLastWrite)
$timeagomin = [math]::Round($timeago.Minutes)
#Write-Verbose "Working with [$fullPath]..."
#Get folder info / sizes
$folderSize = Get-Childitem -Path $fullPath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue
#We use the string format operator here to show only 2 decimals, and do some PS Math.
$folderSizeInMB = "{0:N0}" -f ($folderSize.Sum / 1MB)
$folderSizeInGB = "{0:N2}" -f ($folderSize.Sum / 1GB)
#Here we create a custom object that we'll add to the array
$folderObject = [PSCustomObject]@{
Path = $fullPath
#FolderName = $folderBaseName
'Size(Bytes)' = $folderSize.Sum
#'Size(MB)' = $folderSizeInMB
'Size(GB)' = $folderSizeInGB
#LastwriteTime = $folderLastWrite
TimeAgoMinutes = $timeagomin
}
#Add the object to the array
$folderList.Add($folderObject) | Out-Null
}
#Return the object array with the objects selected in the order specified.
$folderList | sort-object ‘Size(Bytes)’ -Descending | select -First 20 | ConvertTo-Json