Home > database >  Adding folder sizes in Powershell
Adding folder sizes in Powershell

Time:10-01

I am creating a PowerShell script that gets the size of certain folders and then adds the total so it gives us an idea how much we will be transferring. I can get the folder sizes, but cant figure out how to add the numbers together. I have tried Google, looking at like scripts, TechNet scripts for examples and nothing comes close to what I would like to do. This is an example. I can change the folder paths to what I need, but for this, it will work.

"{0:N2} " -f ((Get-ChildItem C:\users\XXXXX\Music -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB) >> "c:\Program Files\XXXXX_Inc\pc_info\folders.txt"
"{0:N2} " -f ((Get-ChildItem C:\users\XXXXX\Desktop -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB) >> "c:\Program Files\XXXXX_Inc\pc_info\folders.txt"
"{0:N2} " -f ((Get-ChildItem C:\users\XXXXX\Documents -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB) >> "c:\Program Files\XXXXX_Inc\pc_info\folders.txt"
"{0:N2} " -f ((Get-ChildItem C:\users\XXXXX\Downloads -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB) >> "c:\Program Files\XXXXX_Inc\pc_info\folders.txt"

Can someone please show me how to add them together??

CodePudding user response:

#Create array with paths you can pass all at once to get-childitem
$paths = @("C:\users\XXXXX\Music","C:\users\XXXXX\Desktop","C:\users\XXXXX\Downloads")
#Get Size
$sizeInGb = (Get-ChildItem $paths -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB
#round
$sizeInGb = [math]::Ceiling($sizeInGb)
#write output
$sizeInGb | set-content C:\size.txt 

#if you need the info per path:
$result = @(
    $paths | %{
        $sizeInGb = (Get-ChildItem $_ -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB
        $sizeInGb = [math]::Ceiling($sizeInGb)
        #build object to return
        $attrsht @{
            path=$_
            sizeInGb=$sizeInGb
        }
        New-Object -TypeName psobject -Property $attrsht
    }
)
#write output
$result | export-csv C:\sizePerPath.csv -NoClobber -NoTypeInformation -Delimiter ";"

CodePudding user response:

$a = ((Get-ChildItem C:\users\XXXXX\Music -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
$b = ((Get-ChildItem C:\users\XXXXX\Desktop -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
$c = ((Get-ChildItem C:\users\XXXXX\Documents -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
$d = ((Get-ChildItem C:\users\XXXXX\Downloads -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
$sum = $a   $b   $c   $d
"{0:N2} " -f $sum >> "c:\Program Files\XXXXX_Inc\pc_info\folders.txt"

CodePudding user response:

Collect the size calculations into an array:

@(
(Get-ChildItem C:\users\XXXXX\Music -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
(Get-ChildItem C:\users\XXXXX\Desktop -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
(Get-ChildItem C:\users\XXXXX\Documents -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
(Get-ChildItem C:\users\XXXXX\Downloads -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
)

Then add and format:

"{0:N2} " -f ((@(
(Get-ChildItem C:\users\XXXXX\Music -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
(Get-ChildItem C:\users\XXXXX\Desktop -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
(Get-ChildItem C:\users\XXXXX\Documents -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
(Get-ChildItem C:\users\XXXXX\Downloads -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
) | Measure-Object -Sum).Sum / 1GB)

If you want to have subtotals for each folder:

$TotalSize = (($FolderSizes = @(
C:\users\XXXXX\Music
C:\users\XXXXX\Desktop
C:\users\XXXXX\Documents
C:\users\XXXXX\Downloads
) | ForEach{ [PSCustomObject]@{
    'Folder' = (Split-Path $_ -Leaf)
    'Size'   = ((Get-ChildItem $_ -File -Recurse) | Measure-Object Length -Sum).Sum
}}) | Measure-Object -Property Size -Sum).Sum

$FolderSizes | ForEach{
    '{0,-12}: {1,6:N2} GB' -f $_.Folder , ($_.Size / 1GB)
}
'{0,-12}: {1,6:N2} GB' -f 'Total Size' , ($TotalSize / 1GB)

Output:

Music       :  28.36 GB
Desktop     :   0.01 GB
Documents   :   1.02 GB
Downloads   :   7.50 GB
Total Size  :  36.89 GB

CodePudding user response:

One more for you, since many have chimed in. ;-}

    Clear-Host
    $SumTotal = 0
    'Total of '
    'Music', 'Desktop', 'Documents', 'Downloads' | 
    ForEach {
       Write-Host "$PSItem is: " -NoNewline -ForegroundColor DarkYellow
            $Total = (
                (
                    Get-ChildItem -Path "$env:USERPROFILE\$PSItem" -Recurse | 
                    Measure-Object -Property Length -Sum -ErrorAction Stop
                ).Sum / 1GB
            )
        "{0:N9}"  -f $Total
        $SumTotal = $SumTotal   $Total
    }
    
    Write-Host "`nThe requested folder(s) size is : $("{0:N2}"  -f $SumTotal) GB" -ForegroundColor Yellow
    # Results
    <#
    Total of 
    Music is: 0.009658834
    Desktop is: 0.000018495
    Documents is: 9.409012590
    Downloads is: 0.009527992
    
    The requested folder(s) size is : 9.43 GB
    #>
  • Related