In our world of computers, objects help us to get stuff done. Objects vary in complexity, but ultimately — they help solve business problems. This article goes deep into real-world practical applications of PowerShell objects.
Defining an Object
I will defer the rigorous defintion of an object until later, but for now, an object is:
Something that gets shit done!
Whilst this definition lacks rigour, it serves its purpose for now. As we progress through this article, we will reshape and redefine this defintion.
As you complete each example and gain further knowledge about objects, redefining the definition of an object will help you synthesise the information quicker.
Unravelling Objects
Let’s unravel objects. Enter the following code into your shell.
$processes = Get-Process | Sort-Object -Property WorkingSet -Descending
Intuitively, this line of code reads like a sentence. It says, in the variable $processes
— store all the processes on the system and sort the processes in ‘descending order of memory used’.
In Figure 1, each process is an object and what Get-Process
really does is get an array of process-objects.
We can get the individual process-objects by using bracket notation to access elements within an array.
$processes
is an array of process-objects and to get the first process-object, we say $processes[0].
Figure 2. Remember, arrays typically start at array[0].
When we use the cmdlet Get-Process,
the cmdlet returns an array of multiple System.Diagnostics.Process
objects. In Figure 3, we see that the first System.Diagnostics.Process
is the process with the ProcessName of Creative Cloud.
PowerShell uses .net types, such as
System.Diagnostics.Process.
We will be covering .net types further in the article.
Types of Objects
When talking about objects, we are really talking about the types of objects.
In the real world, there are different types of objects. Cats and Dogs are objects and whilst they share similar properties and behaviours, they are distinct objects. You would never classify a cat as a dog and similarly — a file-object is not a process-object.
You would never classify a cat as a dog and similarly — a file-object is not a process-object and a process-object is not a date-object…
In the real-world, objects have properties and behaviours. Some properties of a cat might be eye-colour and weight. The cat will also exhibit behaviours such as always landing on its feet. In contrast, dogs do not always land on their feet.
In PowerShell, objects have properties and methods too. So a file-object might have a property named isReadONly
that is set to true or false and a method such as delete
that would delete the file-object.
Let’s redefine what an object is. An object is:
Something that has properties and methods that helps us to get shit done!
Data Types
A discussion of objects would not be complete without a basic understanding data-types.
Many of the things we do in programming revolves around data-types and knowing what data-type we are working with helps us to understand what operations we can do with that data-type.
In PowerShell, data-types fall into two categories. Value-data-types such as integers or floats and reference-data-types, such as strings, arrays and hash-tables.
Find the Type of Object and the Object’s Properties and Methods
To interact with objects, we must first find out what type of object we have.
To do this, we use the Get-Member
cmdlet. Get-Member
conveniently lists the objects type, the object’s properties and the object’s methods. Collectively, the objects properties and methods are known as the object’s members.
So why would we need list an object’s members?
Imagine if you try to classify a cat as a dog and then ask the cat to perform a dog-specific-behaviour. The cat would most likely tell you to go do one because it is a cat and it cannot do dog-specific-behaviours, such as rounding up sheep. In a similar vein, we cannot ask a process-object to give us information about a domain-user because the process-object does not have methods or behaviour to query an active-directory domain controller.
Get-Member
One of the advantages of PowerShell is usability and not memorisation. Get-Member
is the exception and I recommend you commit the cmdlet to memory as it will be extremely useful.
Let’s find the type and members of an object!
Issue the cmdlet Get-Date | Get-Member.
What we are really saying here is get a System.DateTime
object and pass it onto the Get-Member
cmdlet. Get-Member
knows exactly what to do with that System.DateTime
object.
Get-Member
will then list theSystem.DateTime
object’s type, properties and methods. Figure 4. Effortless right?
Get a Property of an Object
Have a look at all the properties and methods that a System.DateTime
object has. Figure 4.
To query the object’s properties or to invoke a method on the object, we must first surround the cmdlet that returns an object in parentheses - like this(Get-Member).
Let’s get a property of aSystem.DateTime
object. We’ll do this by surrounding the cmdlet (Get-Date)
in parenthesis, followed by a (dot) and then, if we know the property name we want, such as Hour, we can type Hour, otherwise we repeatedly hit the tab key to cycle through the System.DateTime
object’s properties and methods. Figure 5.
(Get-Date).Hour
(Get-Date).Hour
will first get a System.DateTime
object and then request the Hour property of that System.DateTime
object. We’ll also get the Millisecond property twice to illustrate how we are dealing with two distinct System.DateTime
objects. Figure 5.
What we are really saying is — hey System.DateTime
object, I want to know what your Hour property is. I also want to know what your Millisecond property is.
Make the Object do Something
Methods are to PowerShell-objects what behaviours are to real-world-objects.
If a cat can meow and stalk prey, it is intuitive to reason that a Date-Time
object cannot meow and stalk prey, but a System.DateTime
object has other behaviours or methods such as IsDaylightSavingTime()
to test if the date is adjusted for daylight savings time.
There are many more methods we can call on a System.DateTime
object, such as ToUniversalTime() which converts the current System.DateTime
object to UTC time. Check out Figure 4 to see if you can reason about what the other methods do.
To find the properties or methods of an object, we pipe the object to theGet-Member
cmdlet with some additional options to retrieve either properties or methods of that object. Figure 6 & Figure 7.
Let’s redefine what an object is:
An object is a type of something that has properties and methods to help us get shit done!