1

I would like to run a PowerShell script to output the folder size two layers deep.

I can do it manually with this:

function Get-Size
{
 param([string]$pth)
 "{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) + " mb"
}

Write-Output "F:\DATA\MEDIA" > .\filesize.txt
Get-size F:\DATA\MEDIA >> .\filesize.txt

Write-Output "F:\DATA\MEDIA\MOVIES" >> .\filesize.txt
Get-Size F:\DATA\MEDIA\MOVIES >> .\filesize.txt

Write-Output "F:\DATA\MEDIA\MUSIC" >> .\filesize.txt
Get-Size F:\DATA\MEDIA\MUSIC >> .\filesize.txt

Write-Output "F:\DATA\MEDIA\TVSHOWS" >> .\filesize.txt
Get-Size F:\DATA\MEDIA\TVSHOWS >> .\filesize.txt

etc etc etc

But then I have to manually type in each folder. The folders are pretty static, but it seems I should be able to automate this somehow.

I'm clearly a noob with PowerShell, and appreciate any help.

Ultimately I'd like to specify a folder and tell it to give size of files in folders two layers deep from where I specify. So from example above if I state:

Get-Size F:\DATA\MEDIA >> .\filesize.txt

Ultimately I'd like an output something like this:

 1,234,567 MB    F:\DATA\MEDIA
   734,567 MB     F:\DATA\MEDIA\MOVIES
   100,000 MB     F:\DATA\MEDIA\MUSIC
   400,000 MB     F:\DATA\MEDIA\TVSHOWS

Where the size of the folder includes all files and subfolders but only shows the folder level that deep in the output.

Thanks for any assistance.

1

Take a closer look at the Get-ChildItem cmdlet help files. It has a depth parameter.

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
<#
# Results

Path
LiteralPath
Filter
Include
Exclude
Recurse
Depth
...
#>
Get-help -Name Get-ChildItem -Examples
Get-help -Name Get-ChildItem -Full
<#
# Results

...
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

Get-ChildItem doesn't display empty directories. When a Get-ChildItem command includes the Depth or Recurse parameters, empty directories aren't included in the output.
...
#>
Get-help -Name Get-ChildItem -Online
0
0

Well I wasn't able to get two folders deep, but one, and this does it's job nevertheless and gives me a clean output. I just thought I'd post for historical purposes. Found some code from around the web that I cobbled together and modified for my purposes. Thanks to original users that made the code:

param(
    [string]
    $pth
    )

function Get-Size
{
    # Set variables to null
    $rootsize = $null
    $sumfoldernum = $null
    $foldertotal = $null

    # Check if path is even valid
    if ( -not ( Test-Path "$pth" ) ) { $pth + " is not a valid path"; ""; ""; return }

    # Output Path for log file
    $pth

    # Check Root Folder Size
    $rootsize = (Get-ChildItem -path $pth -recurse -force | measure-object -property length -sum).sum

    # Store as byte number as well as output file size in MB with fixed spacing
    $rootsizenum = $rootsize
    $rootsize = "{0:n2}" -f ( $rootsize / 1MB )
    ( $rootsize.ToString() ).padLeft(17) + " MB"
    "--------------------"

    # Check SubFolder Sizes
    $colItems = Get-ChildItem -path $pth | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
    foreach ($i in $colItems)
{
    $subFolderItems = ( Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum ).sum

    # Sum all sub folder sizes and output foldersize under root folder heading with individual folder size
    $sumfoldernum = $sumfoldernum + $subFolderItems
    $sumfolder = "{0:N2}" -f ( $subFolderItems / 1MB )
    $sumfolder = ( $sumfolder.ToString() ).padleft(17)
    $sumfolder + " MB" + " -- " + $i.FullName
}

    # Difference root folder from sub folder indicates total size of files in root
        $foldertotal = $rootsizenum - $sumfoldernum
        $foldertotal = "{0:N2}" -f ( $foldertotal / 1MB )
        ( $foldertotal.ToString() ).padleft(17) + " MB" + " -- Files in Root " + $pth
        ""
        ""
}

    # Start main code here
    $datetime = Get-Date -Format "yyyyMMdd_HHmmss"
    $logdir = ".\_logs\foldersize"
    $logfile = "foldersize_$datetime.txt"

    #check if log folder exists, if not then create it and post a file letting user (me) know it has been created
    if ( -not ( Test-Path $logdir ) ) { Write-Output "$(Get-Date) `n`n`Log folder does not exist`n`n`Creating log folder $logdir" | Tee-Object -File ".\FOLDERSIZE_LOGREADME_$datetime.txt" ; md $logdir | Tee-Object -File ".\FOLDERSIZE_LOGREADME_$datetime.txt" -Append}

    # Log Info
    $log = "$logdir\$logfile"

    Write-Output "Start Time: $(Get-Date)" | Tee-Object -File $log
    Write-Output "" | Tee-Object -File $log -Append

    # Execute path input (currently from windows batch)
    Get-Size "$pth" | Tee-Object -File $log -Append

    Write-Output "End Time: $(Get-Date)" | Tee-Object -File $log -Append

    Write-Host "`n`n`*** FOLDERSIZE LOGFILE FOR $pth SAVED TO $log"

Output at least looks clean and simplified:

Start Time: 05/11/2020 22:50:03

F:\DATA\ARCHIVE
           974.46 MB
--------------------
           969.35 MB -- F:\DATA\ARCHIVE\Documents
             5.11 MB -- F:\DATA\ARCHIVE\Games
             0.00 MB -- Files in Root F:\DATA\ARCHIVE


F:\DATA\GAMES
    11,387,344.94 MB
--------------------
       227,673.38 MB -- F:\DATA\GAMES\Battle.net
       333,559.87 MB -- F:\DATA\GAMES\Epic
     1,173,797.71 MB -- F:\DATA\GAMES\GOG
        35,448.62 MB -- F:\DATA\GAMES\Misc
       651,093.42 MB -- F:\DATA\GAMES\Origin
     8,463,138.48 MB -- F:\DATA\GAMES\STEAM
       502,633.46 MB -- F:\DATA\GAMES\UPlay
             0.00 MB -- Files in Root F:\DATA\GAMES


F:\DATA\MEDIA
     1,478,237.33 MB
--------------------
        55,904.54 MB -- F:\DATA\MEDIA\GAMEFILES
        15,865.23 MB -- F:\DATA\MEDIA\MUSIC
        80,535.78 MB -- F:\DATA\MEDIA\SOFTWARE
     1,325,931.78 MB -- F:\DATA\MEDIA\VIDEOS
             0.00 MB -- Files in Root F:\DATA\MEDIA


F:\DATA\OneDrive
       102,142.07 MB
--------------------
            24.01 MB -- F:\DATA\OneDrive\Archive
           917.32 MB -- F:\DATA\OneDrive\Documents
           371.58 MB -- F:\DATA\OneDrive\Files
           992.71 MB -- F:\DATA\OneDrive\Music
        99,836.44 MB -- F:\DATA\OneDrive\Pictures
             0.00 MB -- F:\DATA\OneDrive\Public
             0.00 MB -- Files in Root F:\DATA\OneDrive


F:\DATA\PCBACKUP
     2,648,954.21 MB
--------------------
       699,422.92 MB -- F:\DATA\PCBACKUP\Deathstar
       827,018.05 MB -- F:\DATA\PCBACKUP\Dell1
       747,457.08 MB -- F:\DATA\PCBACKUP\Dell2
       138,852.63 MB -- F:\DATA\PCBACKUP\Enterprise
       236,203.53 MB -- F:\DATA\PCBACKUP\P650HSG
             0.00 MB -- Files in Root F:\DATA\PCBACKUP


F:\DATA\USERS
       872,691.56 MB
--------------------
             0.00 MB -- F:\DATA\USERS\(redacted)
             0.00 MB -- F:\DATA\USERS\(redacted)
       859,441.17 MB -- F:\DATA\USERS\(redacted)
        13,250.39 MB -- F:\DATA\USERS\Public
             0.00 MB -- Files in Root F:\DATA\USERS


End Time: 05/11/2020 22:59:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.