Javatpoint Logo
Javatpoint Logo

PowerShell Sort-object

The sort-object is the cmdlet of a PowerShell, which sorts the objects in an order based on the property values. We can sort the objects by single or multiple properties. The various properties use the hash tables to sort the objects in ascending or descending order.

Syntax



Parameters

-Stable

This parameter was introduced in PowerShell version 6.2.0. The sorted objects are distributed in the order in which they were received when the sort criteria are equal.

-Descending

This parameter is used to sort the objects in the descending order.

-Unique

This parameter is used in sort-object cmdlet to eliminate duplicates and returns only the unique members of the collection. This parameter is case-insensitive.

-InputObject

When this parameter is used, then the sort-object cmdlet receives only one object, which represents the collection.

-Property

This parameter specifies the name of a property that sort-object cmdlet uses to sort the objects. In this parameter, wildcard characters are accepted. If you do not specify a property in the cmdlet, then it sorts on the basis of default properties for an object type.

-Culture

This parameter specifies the cultural configuration, which is used to sort the objects.

-CaseSensitive

This parameter is used in this cmdlet to indicate that the sorts are case-sensitive. By default, they are case-insensitive.

Examples

Example 1: In this example, type the following command to sort the files and folders in the current directory on the basis of name:

In this command, get-childItem gets the files and subdirectories from the current directory. Then, the objects are sent to the Sort-object cmdlet.

Output:

PowerShell Sort-Object

Example2: In this example, type the following command to sort the files and folders in the current directory on the basis of length:

In this command, Sort-Object cmdlet uses the Length parameter to sort the files and directories by a length in ascending order. The file or subdirectory has a maximum length, which is displayed at the end.

Output:

PowerShell Sort-Object

Example3: In this example, type the following command to sort the information of the history of PowerShell session on the basis of Id:

In this command, the Get-History cmdlet is used to get the history objects from the current PowerShell session. The objects are sent down to the Sort-Object cmdlet. The Sort-Object cmdlet uses the Property parameter to sort the objects by Id. The -Descending parameter sorts the command history.

Output:

PowerShell Sort-Object

Example4: In this example, type the following command to sort the names in a text file.

In this command, the Get-Content cmdlet uses the -Path parameter to specify the file and directory name. The file S.txt contains an unsorted list of student names. The objects are sent to the Sort-Object cmdlet. The sort-Object cmdlet sorts the list in the ascending order by default.

Output:

Amisha
Amit
Devansh
Mahesh
Saket
Tarun
Yatin






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA