EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Sort-Object
 

PowerShell Sort-Object

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

PowerShell Sort-Object

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Example:

$students =”Ranjan”,”Ajay”,”Vijay”,”Sujit”,”Ajeet”,”Akash”,”Vikash”
$students

Output:

PowerShell Sort-Object 1-1

This output is not in a sorted way,

$students | Sort-Object

Output:

PowerShell Sort-Object 1-2

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.

sorting for numeric

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:

ascending

$students | Sort-Object

Output:

descending

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:

example

$students | Sort-Object -Unique

Output:

-Unique

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:

PowerShell Sort-Object 1-7

Get-ChildItem -Path ./ranjan/ | Sort-Object

Output:

Get-ChildItem

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:

-Property

Get-Process | Sort-Object -Property WS | Select-Object -Last 10

Output:

PowerShell Sort-Object 1-10

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:

PowerShell Sort-Object 1-11

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-

  1. Working with PowerShell String Functions
  2. Examples of ForEach Loop in PowerShell
  3. Basic Concept and Commands Of Powershell
  4. Difference Between PowerShell vs Bash
  5. Top 16 parameters of PowerShell Get-ChildItem
  6. How to Format Table in Powershell?
  7. Complete Guide to PowerShell Get-Location

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW