• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

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

0
jharen
Asked:
jharen
  • 8
  • 8
1 Solution
 
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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
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
 
jharenAuthor Commented:
Yes Sorry I had an " * " after the folder - it Totally works.
0
 
jharenAuthor Commented:
Awesome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now