9.4. Working With Objects

The outputs of the file system cmdlets appeared to be just like the strings we saw in Bash. However, recall that everything is an object in Windows and PowerShell. Although the shell may format the way they are displayed, all of the outputs from PowerShell commands are in fact objects!

For example, when working with many of the commands, most of the outputs will be Directory, File or String (File contents) object types.

Objects are more tangible than a flat string of characters and bring a new level of depth and efficiency when working from the command-line with PowerShell. They hold properties for quick access to metadata, and they expose methods for common tasks that would require a pipeline of commands to perform in Bash.

9.4.1. Properties & Methods

PowerShell is part of the .NET family of CLS-compliant languages. As a member of the Common Language System, PowerShell is able to access the full suite of .NET class libraries.

The .NET standard library is separated into different namespaces which are like modules of related classes. The root namespace, called the System namespace, contains the base class definitions for fundamental object types like Strings or Arrays.

Because PowerShell and C# are both CLS-compliant languages you will find a lot of similarities between how they are used. Despite some syntactical differences, in both languages properties and methods can be accessed in the same way you are familiar with, using dot notation.

9.4.2. Accessing a Property

Let’s consider one of the simplest object types, those belonging to the String class. Strings have a Length property that can be accessed like this:

Windows/PowerShell
> "dot notation works!".length
19

The equivalent in Bash requires piping through multiple commands:

Linux/Bash
$ echo "dot notation works!" | wc -l

9.4.3. Calling a Method

We can invoke an object’s method in PowerShell the same way as we would in C#.

In the following example, we will access the getType() method attached to a String object. The getType() method shows details about the object it is called on.

Windows/PowerShell
> "hello world".getType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

Note

You can call getType() on any object in PowerShell. As in C#, every object extends the .NET System.Object class which provides the base implementation of getType().

While getType() can give you the type of an object, how might we discover the other type-specific methods and properties that an object has? There is another useful tool built into PowerShell for just this use case.

9.4.4. Discovering Methods and Properties

In the following example, we use the Get-Member cmdlet to access the properties and methods of any given object in PowerShell.

Windows/PowerShell
> Get-Member -InputObject <object>

Let’s use this pattern to view the available properties and methods of a common String object.

Windows/Powershell
> Get-Member -InputObject "a-string"

TypeName: System.String

Name                 MemberType            Definition
----                 ----------            ----------
# ...trimmed
CopyTo               Method                void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
# ...trimmed
TrimStart            Method                string TrimStart(), string TrimStart(char trimChar), string TrimStart(Params char[] trimChars)
Chars                ParameterizedProperty char Chars(int index) {get;}
Length               Property              int Length {get;}

Looking at the output, we can see many things including a property name Length and the handy String methods Split(), Substring(), IndexOf() among the others.

Tip

Between the object getType() method and the Get-Member cmdlet you can discover all of the details about the objects you are working with. Knowing the type and capabilities of an object that cmdlets accept as inputs and produce as outputs will help you when writing more advanced commands and scripts.

For someone new to PowerShell, these are invaluable tools that you should use regularly to familiarize yourself with objects you are working with.