Objects

PowerShell always works with objects. Every action you take in PowerShell occurs within the context of objects. As data moves from one command to the next, it moves as one or more identifiable objects. An object, then, is a collection of data that represents an item. An object is made up of three types of data: the objects type, its methods, and its properties. Whenever you output objects into the PowerShell console, PowerShell automatically converts the rich objects into readable text.

Types, Methods, and Properties

In earlier studies on programming, you probably already know what an object is. It is an instance of a certain class, with properties and objects. Objects in PowerShell are actually quite similar.

The object type tells what kind of object it is. For example, an object that represents a file is a FileInfo object.

The object methods are actions that you can perform on the object. For example, FileInfo objects have a CopyTo method that you can use to copy the file.

The object properties store information about the object. For example, FileInfo objects have a LastWriteTime property that stores the date and time that the file was most recently accessed.

When working with objects, you can use their methods and properties in commands to take action and manage data.

Properties: What an Object is

Properties tells what an object is or has. Please remember when you set PowerShell obejct to a variable, it remains as object, and we can get it's properties. In the following example we will know what properties AppInfo has.

#first set AppInfo to a variable
$a = Get-Service -Name Appinfo
#check is it an object
$a -is [object]
#show all object's properties
$a | Format-List -Property *

Properties of AppInfo

Methods: What an Object 'Can Do'

Methods are things that an object can do. When you output an object to the console, only its properties are converted into text. Methods remain invisible.

Showing methods

To list the methods of an object, use Get-Member and use the parameter memberType with the value method.

$host | Get-Member -memberType Method

Host object methods

Calling a Method

Warning! Before you invoke a method: make sure you know what the method will do. Methods are commands that do something, and what a command does can be dangerous. To call a method, add a dot to the object and then the method name with parenthesis.

Method "Signatures"

Some methods accept different argument types or even different numbers of arguments. To find out which signatures a method supports, use Get-Member again and look at the Definition property.

$info = $host.UI | Get-Member WriteLine
$info.Definition

method signatures

As we can see, there are three different sigantures of writeline()-method. Methods can be called with parameters. In the following example, let's call a method with three parameters.

$host.ui.WriteLine("Red", "White", "Alarm, take a break buddy!")

Call a method of Host object

Creating a New Object

Basically it is possible to create new objects in PowerShell. Syntax is following:

$person = New-Object Object

You can add a property for your object. Syntax is following:

# add a new property named 'Firstname'
Add-Member -memberType NoteProperty -name Firstname -value 'Jack' -inputObject $person

Person object

Using PowerShell objects

Because PowerShell is object-orientated, you can get easily properties from PowerShell objects. Properties describe an object. Object properties are automatically converted into text when you output the object to the console. In the following example, first is printed out host-object and after that one property of the object. Nice and easy! Host object

In the following example two properties are containing Objects. The properties of an object store data, and this data is, in turn, stored in various other objects. For example the property UI in $host seem to be special. When you output $host into the console, all other properties are converted into readable text - except for the properties UI and PrivateData. So if we want to get all data of UI-property, let's try like this. Host object UI

Working with Real-Life Objects

Every PowerShell command returns objects, which is a good thing. However, it is not that easy to handle objects because whenever objects hit the PowerShell console, they will be converted to text and lose a lot of information.

Measure-Object

The Measure-Object cmdlet calculates the property values of certain types of object. Measure-Object performs three types of measurements, depending on the parameters in the command. You use Measure-Object to count objects or count objects with a specified Property. Measure-Object can be used also to calculate the Minimum, Maximum, Sum, StandardDeviation and Average of numeric values. For String objects, it can be also used to count the number of lines, words, and characters.
The following command counts the files and folders in the current directory.

Get-ChildItem | Measure-Object

Measure-Object

In the following command we will count the lines, words and characters of a string variable.

$a = "A quick brown fox jumped over the lazy dog."
$a | Measure-Object -Line -Word -Character

Measure-Object

Storing Results in Variables

So, normally it is not good idea to output command results to the console. Because then we can prevent PowerShell from converting objects into simple text. The console is a malicious place for objects because anything output to the PowerShell console will end up as text. Instead, save the command result in a variable, which is a safe place for objects.
In the following example, Dir is set the variable $mylist, and let's use it for showing the name of the first file.

$mylist = Dir
$mylist[1].Name

mylist

Objects in Pipeline

When commands are combined in a pipeline, they pass information to each other as objects. When the first command runs, it sends one or more objects down the pipeline to the second command. The second command receives the objects from the first command, processes the objects, and then passes new or revised objects to the next command in the pipeline. This continues until all commands in the pipeline run.

The following example shows how objects are passed from one command to the next. We will get all files from user's home directory. Because cmdlet Get-ChildItem returns all items, so files and folders, we will filter only those files that are bigger that one byte ;-).

Get-ChildItem $HOME | Where { $_.Length -gt 1} | Format-Table Name, Length

Get files

Ok, let's try get those items which could be folders with the following script. Please note that we will use a property PsIsContainer.

Get-ChildItem $HOME | Where { $_.PsIsContainer -eq $true} | Format-Table Name

Get folders