• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • 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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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