Sorting a Powershell script output

I have a script that gets the size of folders in a share directory - but I want to sort it from largest to smallest and I cannot figure out how to sort it
function Get-Size($dir=".")
 
{
 
 
 
    $ds = get-item $dir | % { $f = $_;    get-childitem -r $_.FullName | measure-object -property length -sum -ErrorAction SilentlyContinue | select @{Name="Name";Expression={$f}},Sum}
 
    $ds | foreach-object { if ( $_.Sum -le 999KB) {$_.Sum = ([string]::Format("{0:#.##}",($_.Sum)/1KB)) + " KB" ; $_ } elseif ( $_.Sum -le 999MB) {$_.Sum = ([string]::Format("{0:#.##}",($_.Sum)/1MB)) + " MB" ; $_ } elseif ( $_.Sum -le 999GB) {$_.Sum = ([string]::Format("{0:#.##}",($_.Sum)/1GB)) + " GB" ; $_ }}
 
} # End Get-Size
 
# set Alias of 'du' for Get-Size
Set-Alias du Get-Size
 
Get-Size D:\IT\* > C:\Powershellscripts\Directorysize\itfoldersize.txt

Open in new window

jharenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BSonPoshCommented:
You can use sort-object, but understand if you output a string it will not sort as you expect. You want to sort before you format the values in to strings.
0
jharenAuthor Commented:
So where would I insert the sort object?  Thats what I am having trouble with?
0
BSonPoshCommented:
$ds | sort-item Sum -desc | ....
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

jharenAuthor Commented:
$ds = get-item $dir |get-item $dir | % { $f = $_;    get-childitem -r...

Like that
0
BSonPoshCommented:
I would do it before the second foreach... What is the goal of your script?
0
jharenAuthor Commented:
Trying to get a text file that gives me the folder sizes of a shared folder biggest to smallest
0
BSonPoshCommented:
Try this... I think it is a little more efficient and way easier to read :)
function Get-Size($dir=".")
{
    $myobj = "" | Select-Object name,size
    $myobj.Name = $dir
    $DirSize = get-ChildItem $dir -rec -ea 0 | measure-Object length -sum
    if($DirSize.sum -le 1mb)
    {
        $myobj.size = "{0:n2}kb" -f ($DirSize.sum/1kb)
    }
    if(($DirSize.sum -gt 1mb) -and ($DirSize.sum -lt 1gb))
    {
        $myobj.size = "{0:n2}mb" -f ($DirSize.sum/1mb)
    }
    if($DirSize.sum -gt 1gb)
    {
        $myobj.size = "{0:n2}gb" -f ($DirSize.sum/1gb)
    }
    $myobj
}

Open in new window

0
jharenAuthor Commented:
That worked for the Shared folder - but I am looking for the Contents of the shared folder and folder/file sizes of the specific folder - not including subfolders just the main folders in the root ofthe shared folder.
0
BSonPoshCommented:
Ok... then add this

$Parent = Get-item c:\data
foreach($Child in $Parent.GetDirectories())
{
    Get-Size $child.FullName
}

0
jharenAuthor Commented:
To the begining?
0
BSonPoshCommented:
bleh... sorry, I made your script more efficient, but we are now back to square one in regards to sorting.

Let me take another shot at this
0
BSonPoshCommented:
Try this function :)
function Get-Size($dir="."){
    $Parent = Get-item $dir
    $expression = @{n="Size";e={
            if($_.size -le 1mb)
            {
                "{0:n2}kb" -f ($_.size/1kb)
            }
            if(($_.size -gt 1mb) -and ($_.size -lt 1gb))
            {
                "{0:n2}mb" -f ($_.size/1mb)
            }
            if($_.size -gt 1gb)
            {
                "{0:n2}gb" -f ($_.size/1gb)
            }
        }
    }
    @(foreach($Child in $Parent.GetDirectories())
    {
        $myobj = "" | Select-Object Name,Size
        $myobj.Name = $child.FullName
        $myobj.size = (get-ChildItem $child.FullName -rec -ea 0 | measure-Object length -sum).sum
        $myobj
    }) | Sort-Object Size -Desc | Select-Object Name,$expression
}

Open in new window

0
jharenAuthor Commented:
Error

Method invocation failed because [System.Object[]] doesn't contain a method named 'GetDirectories'.
0
BSonPoshCommented:
/shrug It works for me on the three machines I tried against.

How are you calling the function?

Like this?
Get-Size c:\foldername
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jharenAuthor Commented:
Yes Sorry I had an " * " after the folder - it Totally works.
0
jharenAuthor Commented:
Awesome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.