EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Sort-Object

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Sort-Object

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.

Example:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

$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>]

Popular Course in this category
Sale
All in One Data Science Bundle (360+ Courses, 50+ projects)360+ Online Courses | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (3,220 ratings)
Course Price

View Course

Related Courses
Shell Scripting Training (4 Courses, 1 Project)Data Visualization Training (15 Courses, 5+ Projects)

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

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More