Thursday, April 7, 2011

PowerShell Script to Calculate FolderSize

This script will recursively search through a directory looking for matching text patterns in the name and measure the size of the folder in MB.  This will also list the file path.  

...............................

Function FolderSize
{
$startFolder = "E:\users"

$colItems = (Get-ChildItem $startFolder |where {$_.Length}|Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$colItems = (Get-ChildItem $startFolder -Recurse -Include of???arc | 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"
      
    }
    }
    FolderSize | Out-File C:\Temp\FolderSize.txt

................................................................................

No comments:

Post a Comment