Terms of Use For FixedByVonnie

By proceeding to access fixedByVonnie.com, you expressly acknowledge, and agree to, all of the following:

fixedByVonnie.com is a personal website and blog owned by Security Plus Pro LLC, which is being presented for informational purposes only. The views on this website are solely those of the website owner (and not those of any employer or of any professional associations affiliated with the website owner).  Any views expressed in this website and any information presented on this website, or in any of its blog entries, should not be relied on for any purpose whatsoever other than as the personal opinions of the website owner.  The website owner expressly disclaims any and all liability for any information presented on this site.  The owner of this website and its blog posts shall not be held liable, and shall be held harmless, for any errors or omissions in any information or representations contained in this website, or in any of its blog entries.  The website owner also expressly disclaims any liability for the current or future availability of any such information. The website owner makes no representations as to the accuracy or completeness of any information on this website or which may be found by following any link on this website. The website owner shall not be held liable for any losses, injuries, damages, claims, or causes of action, from the display or use of any information on this website or in any of its blog entries. If you use the information on this website, or on any of its blog entries, you do so solely at your own risk.

Say what’s up to PowerShell 5 (Part 17/27) - fixedByVonnie

Say what’s up to PowerShell 5 (Part 17/27)

In the previous post you learned how to extract more information from your objects than the default view allows.  So we learned how to use Select-Object with the -Property parameter to display specific columns (properties) in your output.

Today I’m going to show you how to sort that output.  You probably noticed that PowerShell sorts by the Name property by default.  Which is fine but what happens if you wanted to sort on a different property such as hostname of the computer returning the service?

It’s all about Sort-Object baby!

So remember the pipeline – you can keep stringing cmdlets together to fine tune your output.

So I can do this:

gsv dhcp -computername dc16alx1, ws11alx1 | select -property name, status, machinename | sort -Property machinename

This might look confusing at first but break it down:

  • Get all the DHCP services from computers with hostname dc16alx1 and ws11alx1.
  • Pipe that result to select-object and only display columns for the Name, Status and MachineName (the hostname).
  • Pipe that result to sort and sort the output in ascending order on the machinename property.  By ascending order I mean the natural path of the alphabet (A, B, C, etc… going forward)

Sort-Object property

If you wanted to sort in descending order just use the -Descending parameter.

One thing to keep in mind is that when you pipe an object to Select-Object the object is modified so that it only has the properties specified in the -Property parameter.  So if you try to sort on a property that belongs to the cmdlet but you’ve filtered it out with Select-Object it won’t sort. That’s why it’s always a good idea to pipe the cmdlet to Get-Member so you can think about exactly what kind of data you’re trying to view.

In the next post we’ll look into filtering out specific values using Where-Object (the alias is %).  Using this with Select and Sort will give you the maximum power for building awesome PowerShell scripts and one-liners.

Let’s go!


Connect with Vonnie on Twitter

Posted in Windows, Windows 10 Tagged with: