Measure Home directory - Descending

Hi guys, I need to create a report in Excel format for my boss.
The boss wan’t

This is the script I have modified that I found online.

$rootPath = “E:”
$colItems1 = Get-ChildItem $rootPath
foreach ($i in $colItems1)
{
$colItems = (Get-ChildItem -recurse “$rootPath$i” | Measure-Object -property length -sum)
“{0:N2}” -f ($colItems.sum / 1000MB) + " GB,$i"
“{0:N2}” -f ($colItems.sum / 1000MB) + " GB,$i" >>C:\result.txt
}

The script are great, and gives me the username / name of the folder and the size of the folder.

But it is not sorting the numbers from biggest to smallest.
If i use the descending parameter, It is sorting the name of the folder from A to B and not the numbers. As I want to.

Please help me guys.

I would probably approach it a little differently than the original code - here’s my take:

$report = @()
$rootPath = 'C:\Tools'
$colItems1 = Get-ChildItem -Path $rootPath -Directory
foreach ( $item in $colItems1 ) {

    $folderSize = Get-ChildItem -Path $item.FullName -Recurse | Measure-Object -Property Length -Sum

    $folderInfo = New-Object -TypeName PSObject -Property @{ 
                            'Folder'=$item.FullName; 
                            'FolderSize'=$folderSize.Sum; }

    $report += $folderInfo

}

$report

You could take the content in $report and fork the output to file and screen using something like $report | tee-object ‘report.txt’. You could also convert the folder size to GB or MB or whatever when you look at the output in $report by dividing it by whatever unit of measure you prefer.

Oh, and obviously, you can sort the output

$report | Sort-Object -Property FolderSize -Descending

You need to specify a property, in this case Length, to sort. This will sort properly and export to csv file. In your script, it looks like you are writing to disk for each item and using Get-Childitem multiple times. Multiple writes and other things can cause performance issues. I recommend minimizing your disk writes.

$allitems = $null; $allitems = {@()}.Invoke()

# Put item name and length in object then add to collection
foreach ($item in $colitems){
$itemprop = [PSCustomObject]@{
    Name = $item.Name
    Length = "{0:N2} (GB)" -f ($item.Length / 1GB) }
$allitems.Add($itemprop)
}

# Make one write to disk
$allitems | Sort-Object -Property Length -Descending |
Export-Csv .\result.csv -NoTypeInformation -Append -NoClobber

I took the same approach as the other Matt.

$rootPath = "F:\"

$folders = Get-ChildItem $rootPath -Directory

$objCol = @()

foreach ($folder in $folders) {

    $size = Get-ChildItem $folder -Recurse | Measure-Object -Property Length -sum
    $name = $folder.Name

    $obj = [PSCustomObject] @{
        FolderName = $name
        FolderSize = "{0:N5}" -f ($size.sum/1GB)
    }

    $objCol += $obj
}

$objCol | Sort-Object FolderSize -Descending | Export-Csv F:\report.csv -NoTypeInformation

Hi again guys, I have tried all of the codes you posted, but none of them worked.
The only code that Works, Is the code I posted, but it is not sorting it from big to small numbers.

I made a slight modification to your original script that should work for you. Sorting before using the format operator is the key.

$rootPath = "E:\"
$colItems1 = Get-ChildItem $rootPath
$allitems = $null; $allitems = {@()}.Invoke()
foreach ($i in $colItems1)
{$colItems = (Get-ChildItem -recurse "$rootPath\$i" | Measure-Object -property length -sum)
$itemprop = [PSCustomObject]@{Name = $i.Name; Length = $colItems.sum}
$allitems.Add($itemprop)}

# Export contents
$allitems | Sort-Object -Property Length -Descending | 
Select-Object @{name='Name';exp = {$_.name}},
@{name='Length';exp={"{0:N2} (GB)" -f ($_.Length /1GB)}} |
Export-Csv .\result.csv -NoTypeInformation -Append -NoClobber

Thank you,

But again it Is still not working, this is the output error Messages I geet.

Really don’t know what it is.
I’m using Powershell 3.0 Maybe this has something to do With it.
Would It matter if I use PS Session to do it? On my win 10 Client I have PowerShell 5.0

PS C:> $rootPath = “E:”
$colItems1 = Get-ChildItem $rootPath
$allitems = $null; $allitems = {@()}.Invoke()
foreach ($i in $colItems1)
{$colItems = (Get-ChildItem -recurse “$rootPath$i” | Measure-Object -property length -sum)
$itemprop = [PSCustomObject]@{Name = $i.Name; Length = $colItems.sum}
$allitems.Add($itemprop)}

Export contents

$allitems | Sort-Object -Property Length -Descending |
Select-Object @{name=‘Name’;exp = {$.name}},
@{name=‘Length’;exp={"{0:N2} (GB)" -f ($
.Length /1GB)}} |
Export-Csv C:\resultcontact.csv -NoTypeInformation -Append -NoClobber
Measure-Object : Property “length” cannot be found in any object(s) input.
At line:5 char:55

  • {$colItems = (Get-ChildItem -recurse “$rootPath$i” | Measure-Object -property l …
  •                                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Measure-Object], PSArgumentException
    • FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
      Measure-Object : Property “length” cannot be found in any object(s) input.
      At line:5 char:55
  • {$colItems = (Get-ChildItem -recurse “$rootPath$i” | Measure-Object -property l …
  •                                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Measure-Object], PSArgumentException
    • FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

Hi again, I talked to early.
It actually worked.
You are the best :slight_smile:

This is really great Random Commandline, do you have some tips to me.
How can I learn PowerShell like you, do you have any background I’m impressed.

Recommend me books, Videos and all the Things you can recommend.
I also have a Subscription With pluralsight.
Also I am Norwegian, so my English is a little bit out of shape.

Outstanding Books and Videos are:

Books: by Don Jones and Jeffery Hicks
http://www.amazon.com/Learn-Windows-PowerShell-Month-Lunches/dp/1617291080

http://www.amazon.com/Learn-PowerShell-Toolmaking-Month-Lunches/dp/1617291161/ref=pd_bxgy_14_img_2?ie=UTF8&refRID=18S0T68Q4WDWFQGAEB1P

http://www.amazon.com/PowerShell-Deep-Dives-Jeffery-Hicks/dp/1617291315/ref=pd_sim_14_9?ie=UTF8&dpID=41tUz8JK31L&dpSrc=sims&preST=AC_UL160_SR128%2C160&refRID=0HVEQW9Y7J84ZBFNKNA2

Videos:
https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-30-jump-start-8276

https://mva.microsoft.com/en-US/training-courses/advanced-tools-scripting-with-powershell-3-0-jump-start-8277?l=WOWaGUWy_8604984382

https://mva.microsoft.com/en-US/training-courses/getting-started-with-powershell-desired-state-configuration-dsc--8672?l=ZwHuclG1_2504984382

Excellent, glad I could help. PluralSight has great content. My title is System Engineer, but I consider myself more of a System Administrator. My background consists of Windows/Linux Server, VMware, Active Directory, Exchange, SQL, thin client deployment. I have used or try to use PowerShell in all of these areas when it is useful. David recommended some great books, but before diving into toolmaking I recommend starting with the books below.
PowerShell TFM 4th Edition
(A great overview of what you can do with PowerShell – Powershell install, file/folder manipulation, storing, exporting data, intro in Active Directory, etc )
PowerShell in Action 2nd Edition
(One of my current favorites – This goes into granular detail on why things work and being more efficient in PowerShell – operators, flow control, remoting, error handling, etc)