Powershell Script for folder size and subfolder size

Balayuvaraj M 56 Reputation points
2022-10-26T16:18:58.68+00:00

Hi PS ppl,

I need a powershell script of d dirve folders and its sub folder size. one level down for example d:\ and d:\test.

Kindly help

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-10-27T06:41:58.427+00:00

    Hi @Balayuvaraj M ,

    you can try this to get the result in a CSV file:

    $folder = "C:\Junk"  
    $outputFile = "C:\Junk\foldersize.csv"  
    Out-File -FilePath $outputFile -Encoding utf8 -InputObject "FolderName,Size"  
    $subfolders = Get-ChildItem $folder -Directory | Sort-Object  
    foreach ($folderItem in $subfolders) {  
        $subFolderItems = Get-ChildItem $folderItem.FullName -Recurse -Force -Depth 2 -File | Measure-Object -Property Length -Sum | Select-Object Sum  
        $folderItem.FullName + ”,” + “{0:N3}” -f ($subFolderItems.sum / 1MB) + ” MB” | Out-File -FilePath $outputFile -Append -Encoding utf8  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-10-26T17:50:02.357+00:00

    Hi @Balayuvaraj M ,

    maybe this is helpful to get started:

    $folder = "C:\Junk"  
    $subfolders = Get-ChildItem $folder -Directory | Sort-Object  
    foreach ($folderItem in $subfolders) {  
        $subFolderItems = Get-ChildItem $folderItem.FullName -Recurse -Force -Depth 1 -File | Measure-Object -Property Length -Sum | Select-Object Sum  
        $folderItem.FullName + ” — ” + “{0:N3}” -f ($subFolderItems.sum / 1MB) + ” MB”  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.