Welcome to Sign in | Join | Help
in
Home Blog Forums

The Lazy Admin

PowerShell: Sort

Sponsor


Continuing our look at Powershell we are going finalize our brief introduction with the sort object.
As I mentioned in the previous Powershell article the sort object allows any Powershell output to be sorted consistently using the same logic and syntax structure. While this might not be remarkable at first, if you consider how you sorted data using older CMD based commands it’s a huge step forward. For example the dir command in has the following syntax:

 

The Tasklist command (Lists currently running processes on the computer) doesn’t even have a way to sort the data natively; you need to output it first as text. Power Shell changes this because everything is an object. Because each piece of data is represented as an object we can use a common sort tool for all objects. An analogy would be: If you had a Red Apple, a Green Apple, an Orange and a Red Plumb. Each piece of fruit is an object. Each fruit like objects has properties. The Name Apple is a property for the apples, the color Red is a property for the Apple and the Plumb etc. Now I want to sort the fruit. In the old days with the classic windows command prompt each fruit would need to be sorted using a special pre described method. You would have to get a book on apples to learn how to sort apples, and a book on plumbs to sort them and so on. With PowerShell I can read one book on sorting fruit and I can sort all forms of fruit. The fruit sorting book describes how to sort fruit using their basic properties, I know they have a name, I know they have a colour. If I sort the fruit by colour, I’ll have a pile of Red fruit, a green fruit, and the orange. Since all command output is actually an object, we can pipe any command output to the sort object, and sort by common properties listed in that object. For starters we can sort Directory listing data:

Dir | sort

 

This the same as just typing DIR. Now if we type:

Dir | sort –descending

 

This shows us the same folder list but sorts by name in a descending order rather than an ascending order. Now if we type:

Get-Process | sort –descending

 

We get a list of processes sorted descending. Notice that in the Tasklist command their isn’t any syntax to accomplish this. Finally we can sort output using properties. In the previous image we see Handles, Process ID, and CPU as properties along with the ProcessName which is the default property that is sorted. Typing:

Get-Process | sort id

Outputs all the processes sorted by Id. This ends the intro part on PowerShell. This first few articles were meant as two things, one was for me to get the hang of writing articles for the site, and two, so that there was some sort of framework for people to understand PowerShell. I will try to bring a cookbook solution style from now on. If you have any ideas, or would like to contribute an article don't hesitate to email us here at the Lazy Admin!





Published Thursday, January 04, 2007 9:19 PM by daniel.nerenberg
Filed under:

Comments

No Comments
Anonymous comments are disabled

About daniel.nerenberg

I am an MCT, Consultant based out of Montreal Quebec Canada. As the "new" Lazy Admin on the block I am working to make TheLazyAdmin.com the best website for MS Software tips and tricks out there!


All postings are provided "AS IS" with no warranties, and confer no rights.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.