TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Sort Object

PowerShell Sort Object with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

PowerShell Sort-object

The sort-object is the cmdlet of a PowerShell, which sorts the objects in an order based on the property values. We can sort the objects by single or multiple properties. The various properties use the hash tables to sort the objects in ascending or descending order.

Syntax

Sort-Object
    [-Stable]
    [-Descending]
    [-Unique]
    [-InputObject <PSObject>]
    [[-Property] <Object[]>]
    [-Culture <String>]
    [-CaseSensitive]
    [<CommonParameters>]

Sort-Object
    [-Descending]
    [-Unique]
    -Top <Int32>
    [-InputObject <PSObject>]
    [[-Property] <Object[]>]
    [-Culture <String>]
    [-CaseSensitive]
    [<CommonParameters>]

Sort-Object
    [-Descending]
    [-Unique]
    -Bottom <Int32>
    [-InputObject <PSObject>]
    [[-Property] <Object[]>]
    [-Culture <String>]
    [-CaseSensitive]
    [<CommonParameters>]

Parameters

-Stable

This parameter was introduced in PowerShell version 6.2.0. The sorted objects are distributed in the order in which they were received when the sort criteria are equal.

-Descending

This parameter is used to sort the objects in the descending order.

-Unique

This parameter is used in sort-object cmdlet to eliminate duplicates and returns only the unique members of the collection. This parameter is case-insensitive.

-InputObject

When this parameter is used, then the sort-object cmdlet receives only one object, which represents the collection.

-Property

This parameter specifies the name of a property that sort-object cmdlet uses to sort the objects. In this parameter, wildcard characters are accepted. If you do not specify a property in the cmdlet, then it sorts on the basis of default properties for an object type.

-Culture

This parameter specifies the cultural configuration, which is used to sort the objects.

-CaseSensitive

This parameter is used in this cmdlet to indicate that the sorts are case-sensitive. By default, they are case-insensitive.

Examples

Example 1: In this example, type the following command to sort the files and folders in the current directory on the basis of name:

PS C:\> get-childitem | sort-object

In this command, get-childItem gets the files and subdirectories from the current directory. Then, the objects are sent to the Sort-object cmdlet.

Output:

PowerShell Sort-Object

Example2: In this example, type the following command to sort the files and folders in the current directory on the basis of length:

PS C:\> get-childitem | sort-object -Property Length

In this command, Sort-Object cmdlet uses the Length parameter to sort the files and directories by a length in ascending order. The file or subdirectory has a maximum length, which is displayed at the end.

Output:

PowerShell Sort-Object

Example3: In this example, type the following command to sort the information of the history of PowerShell session on the basis of Id:

PS C:\> get-history | sort-object -Property ID -Descending

In this command, the Get-History cmdlet is used to get the history objects from the current PowerShell session. The objects are sent down to the Sort-Object cmdlet. The Sort-Object cmdlet uses the Property parameter to sort the objects by Id. The -Descending parameter sorts the command history.

Output:

PowerShell Sort-Object

Example4: In this example, type the following command to sort the names in a text file.

PS C:\> Get-Content -Path D:\Powershell\S.txt | Sort-Object

In this command, the Get-Content cmdlet uses the -Path parameter to specify the file and directory name. The file S.txt contains an unsorted list of student names. The objects are sent to the Sort-Object cmdlet. The sort-Object cmdlet sorts the list in the ascending order by default.

Output:

Amisha
Amit
Devansh
Mahesh
Saket
Tarun
Yatin





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf