Need help adding the size of folders with a specific name.

Welcome Forums General PowerShell Q&A Need help adding the size of folders with a specific name.

  • This topic has 8 replies, 4 voices, and was last updated 1 year ago by
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #161685
      Participant
      Topics: 1
      Replies: 4
      Points: 4
      Rank: Member

      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

    • #161705
      Senior Moderator
      Topics: 9
      Replies: 1244
      Points: 4,480
      Helping Hand
      Rank: Community Hero

      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'
      
    • #161718
      Participant
      Topics: 1
      Replies: 4
      Points: 4
      Rank: Member

      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.

       

    • #162152
      Participant
      Topics: 1
      Replies: 4
      Points: 4
      Rank: Member

      Any one?

    • #162156
      Senior Moderator
      Topics: 9
      Replies: 1244
      Points: 4,480
      Helping Hand
      Rank: Community Hero

      The above code is corrected, can you try once and share the output.

    • #163488
      Participant
      Topics: 1
      Replies: 4
      Points: 4
      Rank: Member

      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

       

      • #163524
        Participant
        Topics: 1
        Replies: 64
        Points: 363
        Helping Hand
        Rank: Contributor

        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.

    • #163569
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      @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
      }
      
      
      
    • #164052
      Participant
      Topics: 1
      Replies: 4
      Points: 4
      Rank: Member

      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

Viewing 7 reply threads
  • The topic ‘Need help adding the size of folders with a specific name.’ is closed to new replies.