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??
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