Powershell

How to use PowerShell Sort

PowerShell has a long list of utilities that can be used to automate several tasks or to get the output in the desired format. To get the concise and detailed output, PowerShell does support several cmdlets. For instance, the Sort-Object can be piped with several other cmdlets to get the output in ascending or descending order. The PowerShell Sort is an Alias of the Sort-Object cmdlet and thus does the same action as Sort-object. In this article, the working and usage of PowerShell alias Sort is provided.

How sorting works

The sorting works on the Sort Alias that follows the syntax provided below.

><content-to-sort>| Sort <Parameter><Property>

The properties supported by the PowerShell Sort are provided below.

<Descending> : As the Sort prints the result in ascending order, you can use this option to get the output in descending order.

<Unique>: This parameter eliminates the duplicates and prints the unique values only.

<Property> : You can get the sorted result with respect to the specific property of an object/element on which the sorting is being performed. For instance, the content can be filtered based on the Length, LastTimeWrite property of directories.

<Stable> : If defined, the order remains the same as for the input data.

Let’s use the Sort alias with the help of several examples:

How to use PowerShell Sort

Here, we will explain a few examples that practice the Sort for sorting objects/elements. Starting from the basic functionality, we have created an array and stored a few string expressions in it. Moreover the second command shows the default printing order of Sort alias:

>$s_arr=@('sam', 'Tony', 'Jack', 'Pane')

>$s_arr

The command provided below sorts the string values in the array $s_arr in alphabetically ascending order because no options/parameters are passed in this command.

>$s_arr | Sort

Example 1: Using Sort on integers

The Sort alias can be used to sort the content that contains various data types. We have created a variable that stores several numbers. For instance, the below stated two commands store the numbers and display the stored number in that variable.

>$num= 10, 21, 9, 18, 12, 25, 9, 32, 21, 40, 28

>$num

You would observe from the output that there are few duplicate values, and the order is also not defined. To have a better look at the output, we will apply the Sort alias on the $num with -Descending and -Unique parameters. The output of the command removes all the duplicates, and the order of the numbers is set to descending.

>$num | Sort -Descending -Unique

It can be observed from the above output that numbers are sorted in descending order and duplicate values have been eliminated.

Example 2: Use PowerShell Sort on directories

The directory’s content can also be sorted. In PowerShell, the Get-ChildItem cmdlet is used to get the content inside a directory. The following command practices the use of Sort on Get-ChildItem by sorting the content in descending order with respect to the LastWriteTime property of the directories. The output arranges all the content in descending order according to the modification time of each file/directory.

>Get-ChildItem | Sort -Descending LastWriteTime

The above-stated Get-ChildItem cmdlet was applied to the current working directory. However, you can apply it to any directory on your computer by using the path of the directory. The following command sorts the files/directories of a folder in descending order of length, and the location of that folder is E:\metada.

>Get-ChildItem -Path E:\metadata | Sort -Descending -Property Length

Moreover, the same command can be executed in the following way as well and the result is the same in both cases.

>Get-ChildItem E:\metadata | Sort -Descending Length

Example 3: Using Sort with Hash Tables

Hash tables can be used with Sort to sort the objects with respect to their properties. We are using here the Get-Command cmdlet and hashing table expressions on that cmdlet are practiced. In this regard, the command provided below is practiced that performs the following actions.

  • Groups the content of the CommandType column and sets the Descending parameter to $true
  • And then sorts the Name column in ascending order

There are three categories in the CommandType column and as they are set to Descending order. Hence, the Cmdlet category is shown first followed by Function and Alias. And the content in each category is sorted according to the ascending order of the Name column.

>Get-Command | Sort -Property @{Expression="CommandType"; Descending = $true}, @{Expression= "Name"; Ascending = $true}

Note: Hashing technique is adopted to identify (as a Primary Key) an object from the collection of similar objects. Further, hash functions are used to convert large keys into small keys, and then they are stored in a data structure called hash tables.

Conclusion

PowerShell provides a lot of formatting options that are backed up by cmdlets and functions. The PowerShell sort functionality allows you to get the output in an ordered form that could be either ascending or descending. This post provides a brief explanation of using Sort (an alias of the Sort-Object cmdlet) in PowerShell. The Sort can be used with cmdlets and functions. It could be quite helpful in filtering the content of grouped data using a hashing table in PowerShell. We have presented this guide after experiencing the implementation of sort on various operations of PowerShell.

About the author

Adnan Shabbir