Sort data with PowerShell

Like most command interpreters and script languages, PowerShell lets you sort file contents or command outputs. In addition to numerical and alphabetical sorting, you can also sort based on object properties.

The Sort-Object cmdlet essentially behaves like the old command line's sort.exe. You can pass the contents of a text file through the pipe, and the cmdlet will then sort the text in ascending order:

This example uses the alias sort for Sort-Object to return the contents of names.txt in alphabetical order. If you want to sort in descending order, you have to add the Descending switch. Because PowerShell is case insensitive by default, you have to add the CaseSensitive parameter if you want to distinguish between upper and lower case.

Eliminating duplicates ^

With Get-Unique, PowerShell offers a cmdlet to eliminate duplicates from sorted lists. However, if you first have to sort the data anyway, you can remove multiple occurrences of a particular entry right away through Sort-Object with the unique parameter.

If a command returns multiple objects, each column in the tabular display represents a property. For instance, this applies if you list all files in a folder with Get-ChildItem. In addition to the filename, it will display properties such as size, date of the last correct access, or file attributes.

In such cases, as with the outputs of Get-Process and Get-Service, the result can be either unsorted or sorted by an arbitrary property. However, if you want to sort the output according to particular attributes, you can accomplish this with sort and its Property parameter.

This example sorts the files in the documents folder by their extension. If you would like to arrange the active processes according to their IDs, then this command helps:

Sorting Windows processes by ID

Sorting Windows processes by ID

Sort-Object automatically recognizes that the ID is of the type integer and therefore sorts the output by the ID column's numerical values. If, for some reason, you wish to sort the output alphabetically by the ID, you have to cast the value to a string:

Sorting CSV files ^

PowerShell doesn't just offer these features with structured data returned by cmdlets. It also understands the internal structure of several file formats. This lets you sort their contents with the same pattern. For instance, you can import and parse CSV files with Import-Csv. Afterwards you can sort their contents according to the column titles:

This command would sort a mailing list by the title status. If you want to write the result to a file, you could pipe the output to Set-Content.

Splitting and sorting text files ^

But even if the text files are not available in a common format like CSV, you still can extract the columns and sort their contents. Let's suppose you have a list of mail addresses, and you want to know the number of different domains they belong to:

This command reads the contents of the maildat.txt file and separates the mail addresses using the split method and the delimiter "@" similarly to the Unix cut command. The substrings of the individual lines are available as the elements of an array. In this example we extract the second column (through the index of 1 because counting starts with 0). Afterwards we eliminate all duplicates with sort -unique and calculate the number of remaining entries with measure.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1 Comment
  1. Emmad 7 months ago

    Thank you for the article. I was wondering if running OS sort from CMD line would have the same performance or not? Also, is there a file limit (or key size) limit for this kind of sort? Thanks.


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account