DAY-7: POWERSHELL OBJECT'S















Sorting Objects

Most PowerShell Cmdlets produce objects in a deterministic fashion, which means that they tend to produce objects in the same order every time you run the command. Both service and process, for example, are listed in alphabetical order by name. What if we want to change that.

For example, we want to display a list of processes, which is the biggest consumers of Virtual Memory (VM) at the top of the list. For this, we need to re-order that list of objects based on VM property. PowerShell provides a simple Cmdlet, Sort-Object, which does exactly that:

Get-Process | Sort-Object -property VM

We're hoping that you'll follow this blog and try to run all the given commands to test it yourself. That command isn't exactly what we wanted. It did a sort on VM, but it did so in ascending order, with the largest values at the bottom of the list. By reading help for the sort-object, we see that it has a -descending parameter that should reverse the sort order. We also notice that property parameter is positional, means we don't need to pass the parameter name.

Get-Process | Sort-Object VM -descending

In the event that two processes are using the same amount of Virtual Memory, we'd like to sort them
with process ID.

Get-Process | sort VM, ID  -descending

Selecting Properties

Another useful Cmdlet is Select-Object. It accepts objects from the pipeline, and you can specify the properties that you'd like displayed. This enables you to display properties which are filtered out by PowerShell Configuration rules or to trim down the list to few properties.

Get-process | select-Object -property Name, ID, VM, PM 

The command will show only above parameters on the display screen on PowerShell. From the help figure out which parameter are positional in the Select-object.

Also, one more thing about PowerShell. There are more than 60 properties for Get-process Cmdlet if we want to display all one them using select-object cmdlet, what we would do. Pass all the property name as the value to the parameter -property? No, we can use a wildcard instead of writing all the properties, e,g.

Get-Process | select-object *

Try running the command, but you will see this time you won't see a table, you can see the result is in form of list. Because PowerShell automatically formats the result either in a List or in a table as per the number of properties need to be displayed.

The PowerShell pipeline always contains objects until the last command has been executed. At that time, PowerShell looks to see what objects are in the pipeline and then looks its various configuration files to see which properties to use to construct the onscreen display. It also decides whether that display will be a list or a table, based on some internal rules.

An important fact is that the pipeline can contain many different kinds of objects over a course of single command. E.g

Get-Process | Sort VM -descending | Out-file H:\proc.txt

IN the above example, you start by running Get-Process, which puts process objects into the pipeline. The next command is Sort-Object. That doesn't change what's in the pipeline. The last command is Out-file. Here. PowerShell has to produce output, so it takes whatever is in the pipeline-processes-and formats according to its internal rule set.

Now take a complicated example,

Get-Process | Sort VM -descending | Select-Object Name, ID, VM

In this example, you start by running Get-process, that puts process objects into the pipeline. The next command is Sort-Object. That doesn't change what's in the pipeline. But Select-Object works a bit differently. A process object always has the same number of members. In order to trim down the list of properties,  Select-object can't remove the properties you don't want, because the result wouldn't be a process object anymore. Instead, Select-Object creates a new kind of custom object called PSobject. It copies over the properties you do want from the process, resulting in a custom object being placed into the pipeline. 

Now try the below three commands, you will get the difference.

Get-Process | Get-member

Get-Process | Sort VM -descending | get-Member

Get-Process | Sort VM -descending | Select-Object Name, ID, VM | Get-Member

Notice that, as a part of gm( Get-Member) output, PowerShell Shows you the type name for the object it saw in the pipeline. In the first case, that was System.Diagnostics.Process object, but in the third case the pipeline contains a different kind of object. Those new "selected" objects only contained three properties specified-Name, ID, and VM, plus a couple of system Generated members.

Now we have completed detailed discussion on PowerShell Objects. In our next blog, I will be covering:

1. Advance Pipelining

Comments

Popular posts from this blog

DAY-1 : POWERSHELL…WHAT IT IS>>>AND WHY

Day-5: PowerShell Modules & PSSnapin