PowerShell Beginner’s Guide – Sorting and Selecting Data

In this post we are going to go through sorting, selecting and formatting object data.

To be able to get the right information back in both the PowerShell console and to output to files like txt, csv, html…, it’s import to be able to format the data correctly.

We can sort by using sort-object command, in the below example we can see that the process are sorted by process name by A to Z.

Now we can use sort-object to sort the processes by ID instead of name.

We can added -descending to sort from other way either.

There can be hidden properties that can be useful, to view these we can use the Get-Member and use member type property.

Get-Process | Get-Member -MemberType Property

We can now use the property names with select-object to return the additional properties.

Select-Object has a first and last parameters that let you skip everything but the specified number of objects.

Using the below will only return the first 20 processes.

Get-Process | Select-Object -First 20

Using calculated property in Select-Object is a good way to rename a properties or use math’s to work out a value like the size of a folder or memory used by a process.

To create a calculated property we use @{Name=”Name of property”;Expression={$_.propertyname}}.

Get-Process | Select-Object Name, CPU, @{Name="Memory";Expression={$_.WorkingSet/1KB}}

In the next post we will be going through Variables, Arrays and Hash Tables

Leave a comment