Sort of list display in PowerShell-reverse order

Source: Internet
Author: User
Tags sorts

Order Your Output by easily sorting Objects in PowerShell★ ★ ★★ ★ ★★ ★ ★★★ ★January, by the Scripting Guys //9 Comments
    • 0
    • 0
    • 0

Summary:much of the time, there is no guarantee to the order in which Windows PowerShell returns objects. This blog explains how to fix that issue.

Hey, Scripting guy! I have a problem with Windows PowerShell. It seems that it deliberately randomizes the output. I mean, there seems to being no rhyme or reason to the the-to-the-the-information is-returned from a cmdlet. Am I alone in this frustration, or are there some secret sauce that I am missing? Windows PowerShell is cool, and if I always has to put the data into a Excel spreadsheet just to sort it, then it's not MUC H better than VBScript in my mind. Help me, oh fount of Windows PowerShell wisdom.

-cd

Hello CD,

Microsoft Scripting guy, Ed Wilson, are here. The other day, the Scripting Wife and I were at the first ever Windows PowerShell User Group Meeting in Charlotte, North C Arolina. It was really cool. We love the being able to interact with people The Who love Windows PowerShell as much as we do. Next month, we are have a script-club type of meeting; We encourage people to show up with the Windows PowerShell scripts they is working on, so it'll be a show-and-tell type of meeting.

Use Sort-object to organize output

Anyway, after the user group meeting, if we were all standing around, one of the attendees came up to me and asked me in What order Windows PowerShell returns information. The answer is this there is no guarantee of return order in most cases. The secret sauce is for use of the built-in sorting mechanism from Windows PowerShell itself. In the image is follows, the results from the get-process cmdlets appear to sort on the ProcessName property.

One could make a good argument, the processes should sort on the process ID (PID) or on the amount of CPU time consume D, or on the amount of memory utilized. In fact, it's entirely possible, the supplied by the Process object, someone have a good argume NT for sorting on this particular property. Luckily, custom sorting is easy-to-accomplish in Windows PowerShell. To sort returned objects in Windows PowerShell, pipe the output from one cmdlet to the Sort-object cmdlet. This technique was shown here where the Sort-object cmdlet sorts the Process objects that was returned by the get- Process cmdlet.

get-process | Sort-object ID

The command to sort the Process objects The "ID property and the" output associated with that command is shown In the image that follows.

Reversing the sort order

By default, the Sort-object cmdlet performs a ascending sort-the numbers range from small to large. To perform a descending sort requires utilizing the descending switch.

Note:there is no ascending switch for the Sort-object cmdlets because that is the default behavior.

To arrange the output from the Get-process cmdlet such, the process objects appear from largest process ID to The smallest (the smallest PID is always 0-the Idle process), choose the ID property to sort on, and use the Des cending switch as shown here:

get-process | Sort-object id–descending

The command to perform a descending sort of processes based in the process ID, and the output associated with that command is shown in the image that follows.

When you use the Sort-object cmdlet to Sort output, keep the first position argument are the property or Prope Rties upon which to sort. Because Property was the default means that using the name property in the command is optional. Therefore, the following commands is equivalent:

get-process | Sort-object id–descending

get-process | Sort-object-property id–descending

In addition to using the default first position for the property argument, the Sort-object cmdlets are aliased by s Ort. By using GPS as an alias for the get-process cmdlets, sort as an alias for Sort-object, and a partial par Ameter of des for descending, the syntax of the command was very short. This short version of the command was shown here.

GPS | Sort Id–des

Sorting multiple properties at once

The property parameter of the Sort-object cmdlet accepts a array (more than one) of the properties upon which to the Sor T. This means that I can sort on the process name, and then sort on the working set of memory that's utilized by each pro Cess (for example). When supplying multiple property names, the first property sorts and then the second property sorts.

The resulting output may not always be meet expectations, and therefore, may require a bit of experimentation. For example, the command is follows sorts the process names in a descending order. When that sort completes, the command does a additional sort on the WorkingSet (WS-IS's the alias) property. However, this second sort was only useful when there happen to be multiple processes with the same name (such as the Svchos T process). The command, which is shown this is a example of sorting on multiple properties.

get-process | Sort-object-property name, ws–descending

The figure, which is shown, here illustrates, the output from the command to sort Process objects based on name and W S properties.

When the name and WS properties reverse order in the command, the resulting output was not very useful because the Only sorting of thename property happens when multiple processes has an identical working set of memory. The command that's shown here reverses the order of the WorkingSet and the process name properties.

get-process | Sort-object-property ws, name–descending

The output is shown here shows, there is very little grouping of process names. In this example, adding the name property does not add much value to the command.

Sorting and returning unique items

At times, I might want to see how many different processes is running on a system. To does this, I can filter duplicate process names by using the Unique switch. To count the number of the unique processes that is running on a system, I pipe the results from the Sort-object cmdlets to th E Measure-object cmdlet. This command was shown here.

get-process | Sort-object-property Name-descending-unique | Measure-object

To obtain a baseline this enables me to determine the number of duplicate processes, I drop the Unique switch. This command was shown here.

get-process | Sort-object-property name-descending | Measure-object

Performing a case sensitive sort

One last thing to discuss when sorting items are the casesensitive switch. When used, the casesensitive switch sorts lowercase letters first and then uppercase. The following commands illustrate this.

$a = "alpha", "Alpha", "Bravo", "Bravo", "Charlie", "Charlie", "Delta", "Delta"

$a | Sort-object–casesensitive

When the previous commands run, the output places the lowercase version of the word prior to the uppercase version. This output is appears in the follows.

CD, that's all there-to-sorting with Windows PowerShell. Pipeline Week would continue tomorrow when I'll talk about grouping things with Windows PowerShell.

I invite you to follow me on Twitter and Facebook. If you had any questions, send e-mail to me at [email protected], or post your questions on the official Scriptin G Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Sort of list display in PowerShell-reverse order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.