Measure Home directory - Descending

This topic contains 11 replies, has 5 voices, and was last updated by Profile photo of random commandline random commandline 10 months, 3 weeks ago.

  • Author
    Posts
  • #34072
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    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.

  • #34075
    Profile photo of Matt Allison
    Matt Allison
    Participant

    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.

  • #34076
    Profile photo of Matt Allison
    Matt Allison
    Participant

    Oh, and obviously, you can sort the output

    $report | Sort-Object -Property FolderSize -Descending
    
  • #34077
    Profile photo of random commandline
    random commandline
    Participant

    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
    
  • #34078
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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
    
  • #34115
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    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.

  • #34129
    Profile photo of random commandline
    random commandline
    Participant

    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
    
  • #34168
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    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: (:) [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: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

  • #34171
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

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

  • #34174
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    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.

  • #34176
    Profile photo of David DeHerrera
    David DeHerrera
    Participant
  • #34181
    Profile photo of random commandline
    random commandline
    Participant

    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)

You must be logged in to reply to this topic.