Updated March 24, 2023
Introduction to PowerShell Sort-Object
In this article, we’ll discuss different PowerShell Sort-Object. Sorting is an essential work in any programming language. Sorting is always used when we need the results in a certain pattern to analyze the data. Let me tell you one example, suppose you have some student’s details and you want to see the result in alphabetically sorted order.
Example:
$students =”Ranjan”,”Ajay”,”Vijay”,”Sujit”,”Ajeet”,”Akash”,”Vikash”
$students
Output:
This output is not in a sorted way,
$students | Sort-Object
Output:
the output of execution can be seen in the image below. Initially, the result was,” Ranjan”,” Ajay”,” Vijay”,” Sujit”,” Ajeet”,” Akash”,” Vikash” after Sorting we got the result Ajay Ajeet Akash Ranjan Sujit Vijay Vikash. Here they sorted in Alphabetical order. We can do sorting for numeric and also for any directory according to their properties. We will discuss more examples and. See the screen below for this example.
Syntax and Parameters of PowerShell Sort-Object
A very simple syntax is {Array ,directories} | Sort-Object .We are using “|” as a separator in PowerShell, you can write as many commands as you want with “|” separated. Sort-Object has various other parameters like Properties(Defining the sort property criteria like length, size, and type, etc).
Attribute for sorting | Sort-Object | command 1 | command 2 | command 3
Syntax #1:
Sort-Object
[-Stable]
[-Descending(order in which we need output)]
[-Unique(use this to remove all duplicate and return unique values)]
[[-Property] <pass all the properties on which you want to do sorting like length,date,cpu uses etc>]
[-CaseSensitive(Sorting either case sensitive or case non sensitivity )]
[<CommonParameters>]
Syntax #2:
Sort-Object
[-Descending(order in which we need output)]
[-Unique(use this to remove all duplicate and return unique values)]
-Top <Integer values>
[[-Property] <pass all the properties on which you want to do sorting like length,date,cpu uses etc>]
[-CaseSensitive(Sorting either case sensitive or case non sensitivity )]
[<CommonParameters>]
Syntax #3:
Sort-Object
[-Descending(order in which we need output)]
[-Unique(use this to remove all duplicate and return unique values)]
-Bottom <Integer values>
[[-Property] <pass all the properties on which you want to do sorting like length,date,cpu uses etc>]
[-CaseSensitive(Sorting either case sensitive or case non sensitivity )]
[<CommonParameters>]
Parameters:
- -Bottom: It defined the Object numbers going to get from the end of the array(sorted object).
- -CaseSensitive: We use this command if we need output sorted element without considering the case of alphabets, that means small or capital letter it will consider the same. Always remember by default it is not case sensitive.
- -Path: It denotes the path for which we are running our sort command.
- Descending: Descending means order of sorting, So suppose you have many numbers and you want to see the result in descending order then we can use descending command along with Sort-Object. And if you do not define Descending along with Sort-Object command it will consider as Ascending. If we want to sort multiple things at once than we can use a hash table.
- -Property: Here property means property of the element on which we wanted to do sorting, We can do sorting on any specific properties of file systems. So, for example, we can do sorting of files on the basis of size, date, etc. Many times when you are searching for something you may need to find the latest file, in such a situation you can do sorting along with length properties. We can see it in example 5.
- -Stable: It’s return output in the same order in which it received input when the condition for sorting will be equal.
- -Top: It defines the Object numbers return from the start of the sorted array.
- -Unique: Its name is enough to clarify its meaning, It always returns a unique result after sorting of objects.
Examples to Implement PowerShell Remove-Item
Below are the examples for implementing the PowerShell Remove-Item:
Example #1
In the Below example, We have created an array of the numbers, we can see the numbers are not in the order, but after sorting we can see smaller numbers came on the top and biggest number on the bottom. This is its default behavior, we can specify sort order bypassing descending or ascending in the property.
$students =12,34,23,90,23,45,90,88,77,70
$students
Output:
$students | Sort-Object
Output:
Example #2
Here we have random unsorted numbers along with duplicates, so in this command, we are removing all duplicates along with sorting them, see below example.
$students =12,34,23,90,23,45,90,88,77,70
$students
Output:
$students | Sort-Object -Unique
Output:
Example #3
In this example, we are fetching all child files of ranjan1 directories and sorting them. We can see below is the output in the tabular form, which returning Date of last modified and length etc .Once you will be handy to this command you will love to use it to find anything in your file systems.
Get-ChildItem -Path ./ranjan1/ | Sort-Object
Output:
Get-ChildItem -Path ./ranjan/ | Sort-Object
Output:
Example #4
Let me give you one very Useful example many times when you are using your system(computers) you may feel that your system is running very slow. So to understand the root cause of the slow system we can check which process is taking how much CPU and memory. And once you know the heaviest process, you can simply kill those running processes that are taking more CPU. In the below two examples we are sorting them in order of highest CPU uses. In below two screens we are fetching the first 5 and 10 highest CPU consuming processes. You can also try to find process running for the longest time or process started recently etc.
Get-Process | Sort-Object -Property WS | Select-Object -Last 5
Output:
Get-Process | Sort-Object -Property WS | Select-Object -Last 10
Output:
Example #5
Let’s take another important example, here we are sorting according to the length of the file system . many times, it is possible that your directory contains thousands of files but you want to see only those which are large in size. So In the below example, we are sorting bypassing length attribute in -Property command. You can also sort them with combined multiple sorting conditions. Please see the screen below for a better understanding.
Get-ChildItem -Path ./Desktop/ -File | Sort-Object -Property Length
Output:
Conclusion – PowerShell Sort-Object
PowerShell Sort-Object, I am hoping that this tutorial was good enough to give a brief to Sort-Object. From this tutorial, we came to know some good ways to represent and search files. It allows us to sort the array and directory both.
Recommended Articles
This is a guide to PowerShell Sort-Object. Here we discuss the syntax, parameters, and different examples to implement Remove-Item. You may also look at the following articles to learn more-