PowerShell Logo Small

Sort-Object



This is the built-in help made by Microsoft for the command 'Sort-Object', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Sorts objects by property values.

SYNTAX


Sort-Object [[-Property] <Object[]>] [-CaseSensitive] [-Culture <string>] [-Descending] [-InputObject <psobject>] [-Unique] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object.

You can specify a single property or multiple properties (for a multi-key sort), and you can select a case-sensitive or case-insensitive sort. Yo
u can also direct Sort-Object to display only the objects with a unique value for a particular property.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113403
Group-Object

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

C:\PS>get-childitem | sort-object

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/13/2005 4:24 PM 0 0
-a--- 9/6/2005 4:19 PM 12 a.csv
-a--- 9/21/2005 3:49 PM 529 a.Ps
-a--- 8/22/2005 4:14 PM 22 a.pl
-a--- 9/27/2005 10:33 AM 24 a.txt
-a--- 9/15/2005 10:31 AM 398 a.vbs
-a--- 7/21/2005 12:39 PM 37066 a.xml
-a--- 8/28/2005 11:30 PM 5412 a.xslt
-a--- 10/25/2005 1:59 PM 125 AdamTravel.txt
-a--- 7/21/2005 9:49 AM 59 add2Num.Ps
-a--- 8/29/2005 5:42 PM 7111 add-content.xml
-a--- 9/21/2005 12:46 PM 8771 aliens.Ps
-a--- 8/10/2005 2:10 PM 798 array.xml
-a--- 8/4/2004 5:00 AM 110 AUTORUN.INF
-a--- 9/6/2005 4:20 PM 245 b.csv
...



Description
-----------
This command sorts the subdirectories and files in the current directory. Because no properties are specified, the files and directories are sort
ed in ascending alphabetical order by their default sort property, Name.








-------------------------- EXAMPLE 2 --------------------------

C:\PS>get-childitem | sort-object -property length

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/3/2006 5:35 PM 2 pref.txt
-a--- 9/6/2006 3:33 PM 15 count.txt
-a--- 7/26/2006 10:01 AM 30 filenoext
-a--- 8/18/2006 9:02 AM 52 temp.ps1
-a--- 8/18/2006 9:02 AM 52 temp.msh
-a--- 9/6/2006 3:33 PM 56 fivewords.txt
-a--- 7/26/2006 9:28 AM 80 date.csv
-a--- 7/29/2006 7:15 PM 84 test2.txt
-a--- 7/29/2006 7:15 PM 84 test.ps1



Description
-----------
This command displays the files in the current directory in ascending order by file length.








-------------------------- EXAMPLE 3 --------------------------

C:\PS>get-process | sort-object -property WS | select-object -last 5

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
1105 25 44236 18932 197 93.81 2032 iexplore
2526 66 37668 36836 221 393.27 868 svchost
974 19 22844 45928 371 88.39 3952 WINWORD
1371 22 42192 61872 323 75.75 1584 INFOPATH
2145 58 93088 70680 619 396.69 3908 OUTLOOK



Description
-----------
This command displays the five processes on the computer with the greatest memory use based on the size of their working sets.

The command uses the Get-Process cmdlet to get a list of processes. It uses a pipeline operator (|) to send the results to the Sort-Object cmdlet
, which sorts the objects in working-set order.

Another pipeline operator sends the results to the Select-Object, which displays only the last five items in the list.








-------------------------- EXAMPLE 4 --------------------------

C:\PS>get-history | sort-object -descending

Id CommandLine
-- -----------
51 get-history | sort -descending
50 get-history | sort -descending
49 get-history | sort -descending
48 get-history | sort -descending
47 get-history | sort -descending
46 get-history | sort -descending
45 get-history | sort -descending
44 cd $pshome
43 get-childitem | sort-object
42 gci *.txt



Description
-----------
This command sorts HistoryInfo objects using the Id property as the default key.








-------------------------- EXAMPLE 5 --------------------------

C:\PS>C:\PS> get-service | sort-object -property `
@{Expression="Status";Descending=$true}, `
@{Expression="DisplayName";Descending=$false}

Status Name DisplayName
------ ---- -----------
Running ALG Application Layer Gateway Service
Running Ati HotKey Poller Ati HotKey Poller
Running wuauserv Automatic Updates
Running BITS Background Intelligent Transfer Ser...
Running Client for NFS Client for NFS
...
Stopped clr_optimizatio... .NET Runtime Optimization Service v...
Stopped Alerter Alerter
Stopped AppMgmt Application Management
Stopped aspnet_state ASP.NET State Service
Stopped ATI Smart ATI Smart
Stopped ClipSrv ClipBook



Description
-----------
This command displays the services on the computer in descending Status order and ascending DisplayName order.

The command uses the Get-Service cmdlet to get the services on the computer. It uses a pipeline operator (|) to send services to the Sort-Object
cmdlet.

To sort one property in ascending order and another property in descending order, the command uses a hash table for the value of the Property par
ameter. The hash table uses an Expression key to specify the property name and an Ascending or Descending key to specify the sort order.

The resulting display, which sorts the Status values in descending order, lists properties with a Status value of "Running" before those with a S
tatus value of "Stopped". When sorted in ascending order, "Stopped" appears before "Running", because Status is an enumerated property in which t
he value of "Stopped" (1) is less than the value of "Running" (4).








-------------------------- EXAMPLE 6 --------------------------

C:\PS>get-childitem *.txt | sort-object -property @{Expression={$_.LastWriteTime - $_.CreationTime}; Ascending=$false} | Format-Table LastWriteTi
me, CreationTime

LastWriteTime CreationTime
------------- ------------
2/21/2006 10:22:20 AM 10/3/2005 4:19:40 PM
2/27/2006 8:14:24 AM 2/23/2006 10:41:08 PM
2/24/2006 1:26:19 PM 2/23/2006 11:23:36 PM
1/5/2006 12:01:35 PM 1/5/2006 11:35:30 AM
2/24/2006 9:25:40 AM 2/24/2006 9:22:24 AM
2/24/2006 9:40:01 AM 2/24/2006 9:39:41 AM
2/21/2006 10:21:30 AM 2/21/2006 10:21:30 AM



Description
-----------
This command sorts text files in descending order by the time span between CreationTime and LastWriteTime.








-------------------------- EXAMPLE 7 --------------------------

C:\PS>get-content servers.txt

localhost
test01
server01
server02
localhost
server01

C:\PS> get-content servers.txt | sort-object -unique
localhost
server01
server02
test01



Description
-----------
These commands sort the names of servers in a text file. The second command uses the Sort-Object cmdlet with the Unique parameter to return a sor
ted list without duplicates.