erkan
June 18, 2019, 6:42am
1
Hi All,
I have been assigned the task of looking through a folder recursively and only grabbing certain folders by name and then calculating the size of those folders.
So far i have been able to do this per folder and display the size of each folder on the console, but i really am struggling to find a way to sum the size of all of the folders into one figure.
I have tried to do this:
$Folders = (Get-ChildItem 'F:\Profile\' -Recurse -Force | where {$_.name -eq 'xaprofile' -or $_.name -eq 'profile' -or $_.name -eq 'lxaprofile'})
foreach ($folder in $Folders) {
$RawSize = $folder | Get-ChildItem -Recurse -Force -File | Measure-Object -Property Length -Sum
#Format to two decimal places
"{0:N2}" -f ([int32]$RawSize.sum /1MB)
}
Any steers would be much appreciated.
Thanks,
Erkan
How about a oneliner.
$PathToCheck = 'F:\Profile\'
Get-ChildItem -Path $PathToCheck -Recurse -Include xaprofile,profile,lxaprofile | ForEach-Object -Process { Get-ChildItem -Path $_.FullName -Recurse } | Measure-Object -Property Length -Sum | Select-Object -Property @{E={"{0:N2}" -f ([int32]$_.Sum/1MB)};L='Length'
erkan
June 18, 2019, 8:18am
3
Hey, thanks for the quick response. It doesn’t seem to output anything, all i get is:
Length
------------
I also had to add an extra } on the end and remove the extra } after the first -recurse.
The above code is corrected, can you try once and share the output.
erkan
July 1, 2019, 7:32am
6
Still the same, all i get is this as the output:
Length
Have you tried this yourself on a folder containing subfolders? Just doesn’t work as is.
Thanks,
E
Try this:
foreach ($folder in $Folders) {
$RawSize = $folder | Get-ChildItem -Recurse -Force -File | Measure-Object -Property Length -Sum
#Format to two decimal places
$rawsize2 = $rawsize2 + $rawsize.sum
"{0:N2}" -f ([int64]$RawSize.sum /1MB)
}
"{0:N2}" -f ([int64]$RawSize2 /1MB)
I just set another variable $rawsize2, that I build inside the loop. Let me know if its what you’re looking for.
nitbha
July 1, 2019, 11:59am
8
@erkan13 Without disturbing your original logic you can do something like below
$Folders = (Get-ChildItem 'F:\Profile\' -Recurse -Force | where {$_.name -eq 'xaprofile' -or $_.name -eq 'profile' -or $_.name -eq 'lxaprofile'})
foreach ($folder in $Folders) {
$RawSize = $folder.fullname | Get-ChildItem -Recurse -Force | Measure-Object -Property Length -Sum
$hash=[PSCustomObject]@{
$($folder.name)=$("{0:N2}" -f ([int32]$RawSize.sum /1MB))
}
$hash
}
erkan
July 3, 2019, 2:22pm
9
Excellent and thank you both for responding. an0nemus09’s code worked perfectly. I got nothing outputted from your code Evila. Thank you very much for taking the time to help., i don’t use Powershell enough these days and have gotten very rusty so much appreciated.
Erk