Passing properties down pipeline with Measure-Object

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Eug Eug 3 months, 2 weeks ago.

  • Author
    Posts
  • #63645
    Profile photo of Eug
    Eug
    Participant

    I am trying to write a PowerShell command to return the size of each immediate sub-folder. The script should return the size of the size of the file layer of subfolders and for each subfolder the sum of all of its decedents. I have written the following and it give me the info, however in doing so I lost the folder name property.
    gci -Directory | ForEach-Object {Get-ChildItem -file $_ -Recurse | Measure-Object -sum length} |Format-Table
    Namely I need the name first layer displayed along side the sum of length of all of its subsequent decedents.

    How do I achieve this?

  • #63661
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    The following could work for you:

    Get-ChildItem -Directory | ForEach-Object {
        [PSCustomObject] @{
            FolderPath = $_.FullName
            FolderSize = Get-ChildItem -Path $_.FullName -File -Recurse -ErrorAction SilentlyContinue | 
                Measure-Object -Sum Length | Select-Object -ExpandProperty Sum
        }
    }
    
    • #63712
      Profile photo of Eug
      Eug
      Participant

      Good stuff Daniel, thanks, that is exactly what I need.

You must be logged in to reply to this topic.