I have a script to check the size of a folder and all of its sub-folders and it does what I need but it throws errors if the folder has size 0. I would like to add some logic in but I cant seem to find a good way to do it, thanks in advance for the help.
The script is:
$startFolder = "C:\"
$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object $_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
{
$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}
this worked for me without errors: