'Powershell Subfolder sizes (MB/GB) to out-grid

So I'm trying to combine #1 and #2 to get the sub-folder sizes and put into an out-gridview with folder name and MB/GB sizes. #3 works but it shows @name... vice just size mb/kb. looking for helping combining the two into one.


#1
Get-ChildItem -Path "C:\" -Recurse -Force -ErrorAction SilentlyContinue | 
Measure-Object -Property Length -Sum | 
Select-Object @{Name="Size(kB)";Expression={("{0:N2}" -f($_.Sum/1kb))}}, 
@{Name="Size(MB)";Expression={("{0:N2}" -f($_.Sum/1mb))}}, 
@{Name="Size(GB)";Expression={("{0:N2}" -f($_.Sum/1gb))}} | 
Out-GridView

#2
Get-ChildItem -path "C:\" -Directory -Recurse | 
Select-Object FullName | 
ForEach-Object -Process{New-Object -TypeName PSObject -Property @{Name =$_.FullName;Size = (Get-ChildItem -path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | 
Measure-Object -Property Length -Sum ).Sum/1kb}} | 
Select-Object Name, @{Name="Size(KB)";Expression={("{0:N2}" -f($_.Size))}}|
Out-Gridview



#3 - combine 1/2 and set variable path
#set path
$path = "c:\"

#GET Sizes sort MB/GB
Get-ChildItem -path "$path" -Directory -Recurse | 
Select-Object FullName | 
ForEach-Object -Process{New-Object -TypeName PSObject -Property @{Name =$_.FullName;Size = (Get-ChildItem -path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | 
Measure-Object -Property Length -Sum)| 
Select-Object Name,
@{Name="Size(MB)";Expression={("{0:N2}" -f($_.Sum/1mb))}}, 
@{Name="Size(GB)";Expression={("{0:N2}" -f($_.Sum/1gb))}}  
}
}|
Out-GridView


Solution 1:[1]

Break down your code and you will see where the issue is, this one of many reasons why indentation is important:

New-Object -TypeName PSObject -Property @{
    Name = $_.FullName
    Size = (
        Get-ChildItem -path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue | 
        Measure-Object -Property Length -Sum
    ) | Select-Object 'Name',
            @{Name="Size(MB)";Expression={("{0:N2}" -f($_.Sum/1mb))}},
            @{Name="Size(GB)";Expression={("{0:N2}" -f($_.Sum/1gb))}}  
}

The object produced by New-Object is the one passed to Out-GridView, this object will consist of 2 properties, Name and Size.

The Value of the Size property will be an object with 3 properties, Name, Size(MB) and Size(GB).

The object would look similar to this when seen on the console:

Name Size
---- ----
foo  @{Name=foo; Size(MB)=0.00; Size(GB)=0.00}

# Size is stringified when seen in the console:
[pscustomobject]@{
    'Name'     = 'foo'
    'Size(MB)' = '0.00'
    'Size(GB)' = '0.00'
} -as [string]

# => @{Name=foo; Size(MB)=0.00; Size(GB)=0.00}

Here is an easier way you can get your desired output. Note the use of [pscustomobject] to instantiate a new object, instead of New-Object. This method is faster and cleaner, however is only available on PowerShell v3.0 and newer versions.

Get-ChildItem -Path C:\ -Directory -Recurse | ForEach-Object {
    $size = Get-ChildItem -Path $_.FullName -File -Force -ErrorAction SilentlyContinue |
        Measure-Object -Property Length -Sum
    [pscustomobject]@{
        "FullName" = $_.FullName
        "Name"     = $_.Name
        "Size(MB)" = "{0:N2}" -f ($size.Sum / 1Mb)
        "Size(GB)" = "{0:N2}" -f ($size.Sum / 1Gb)
    }
} | Out-GridView

I believe you're looking to get the folder's size based on the files inside them (non recursive), hence removing -Recurse and adding -File:

$size = Get-ChildItem -Path $_.FullName -File -Force -ErrorAction SilentlyContinue |
    Measure-Object -Property Length -Sum

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1