Powershell Objects

Cmdlets output objects, and can consume objects.

Objects have properties. Properties can be read-only, or modifiable.

Not all properties are shown by default when you view an object

Objects have methods, which may have parameters. Cmdlets are often associated with methods, but methods can be invoked manually too.

Get-Member lists all properties and methods of an object.

 

Can filter to just methods or properties etc,

Select-Object works with any object.

Pipe an object into Select-Object to view it indifferent ways, -Property says which properties (columns) to show

OR to get all properties can do this, but it will show in a non-table way:

To list all the properties for the actual object and not the type.

Sorting

pipe through Sort

Select a property to sort on , and ascending or descending

Or

Sometimes the sort might appear to not work. This can be because the sort is not working on the text value, but an underlying numeric value. In this case, you need to convert the property to a text value and sort on that instead, e.g.

 

Filtering

To get a subset of results, use Where-Object (alias = Where), specifying the property and condition to filter on, e.g.:

 

Operators are:

-eq

-lt

-gt

-ceq (case sensitive)

-like (wildcard match)

-notlike (wildcard match)

-match (regex)

 

The shorthand version is more widely used and more flexible when it comes to complicated expressions

$_ means the current object in the pipeline

 

Grouping

Objects can be grouped based on properties.

This is now a GroupInfo Object, so if we wanted to get hold of the first running service from here, we would need to expand the group property to get the service objects in it, and then get the first one of those:

 

Piping to Out-GridView pops out a GUI window with resizable columns etc