Powershell Script for folder size and subfolder size

Balayuvaraj M 31 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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,174 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 77,971 Reputation points MVP
    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 77,971 Reputation points MVP
    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