in

WindowsWindows Loving ItLoving It

[WORKING] PowerShell Sort | Descending | Unique | Case Sensitive

Learn how to sort an array in PowerShell. Sort in Ascending, Descending, Unique and case sensitive orders and features. Remove only one element from array.

PowerShell sort array

In this post, we are going to see the different ways we can sort an array in PowerShell.   To sort an array in PowerShell, we can pipe the list of elements to the sort-object command let.  The default order of sorting is ascending order and so in-order to sort in descending order, we have to use the descending switch.

Jump to:

People also search for:

PowerShell sort string array:

Below is the script that demonstrates how to sort a string array in PowerShell.

$strarray = @(“red”,”blue”,”green”,”yellow”)

$strarray | sort

The output of the above script is shown in the image below.

PowerShell sort array

We can sort the array in the descending order using the below script as mentioned earlier.

$strarray | sort -Descending

The output of the above script is given here.

PowerShell sort in descending order

PowerShell sort array with Case Sensitivity:

For this section, we are going to use a more appropriate array as given below.

$csarray = @(“Red”,”red”,”Yellow”,”orange”,”Green”,”green”)

We will pipe the array to the sort commandlet and check the output.

$csarray | sort

The output is shown below.

PowerShell sort array

Now we will pipe the array to the sort command let and apply the descending switch.

$csarray | sort -Descending

Now let us apply the case sensitive switch as shown below.

$csarray | sort -CaseSensitive

The output of the above script is shown in the image below.

PowerShell sort array case sensitive

PowerShell sort array and return only unique elements:

This is one of the more useful features of sorting an array in PowerShell and it is to sort and return only the unique elements.  It is done using the -unique switch.  It is very easy to use as shown in the below script.

Let us first add some redundant elements to our array.

$csarray += “Yellow”

$csarray += “red”

Let us apply the unique switch and check the output.

$csarray | sort -unique

The output of the above script is given below.

unique switch in PowerShell

Finally, we need to check how to delete elements from arrays:

There is no straight forward way to delete elements from arrays.  But if we want to remove one specific element from an array, we can cast it to an array list and use the remove() method.

We can cast the array to the array list using the type modifier given below.

[system.collections.arraylist]

We use the type modifier on our array as shown below.

$csarray = [system.collections.arraylist]$csarray

Now we can use the remove() method available in the array list and remove the yellow color.

$csarray.Remove(“Yellow”)

We can check the output as shown below.

remove element from array list

Verdict:

We can sort arrays in PowerShell very easily.  We have to use only the sort command let to achieve this.  The default sorting order is ascending order and the descending order can be achieved using the -descending switch.

If you want to know more about PowerShell check out our other articles on PowerShell.

Leave a Reply

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