Understanding the Basics of PowerShell: Objects

The core strength of PowerShell lies in its use of objects. If you’re new to PowerShell—or scripting in general—understanding how objects work can enhance your scripting capabilities. To get started, we’ll explore the basics of objects in PowerShell and how you can use them to make your scripts efficient and powerful.

What is an Object in PowerShell?

In PowerShell, everything is an object. From simple strings and numbers to complex data structures and command outputs, these are all represented as objects. This means that you can manipulate and interact with these elements using object-oriented principles.

Unlike simple text-based data, objects are more complex and versatile. They consist of:

  • Properties: These are data fields that store information about the object.
  • Methods: These are functions or actions that the object can perform.

Think about a real-world object—like a car. A car has properties like color, make, and model. And, it has methods for actions like start, stop, and accelerate. Now, think about a FileInfo object on your computer. A FileInfo object represents a file and includes properties like Name, Length, and CreationTime. And it has methods for actions like deleting or copying the file.

An easy way to determine the properties and methods of a PowerShell object is to use the Get-Member cmdlet. You can use this to return the various MemberTypes of the object for both properties and methods. Here is an example showing the various Properties and Methods of a Windows service.

PowerShell Services Example 1

PowerShell Services Example 2

Why Objects Matter in PowerShell

Understanding objects is crucial for efficient PowerShell scripting. By properly leveraging objects in your scripts, your scripts will gain:

  • Consistency: PowerShell’s object-oriented nature provides a consistent way to work with data.
  • Power and Flexibility: Objects offer powerful features like properties, methods, and inheritance, enabling complex operations.
  • Efficiency: By leveraging object pipelines, you can streamline your scripts and improve performance.
  • Integration: PowerShell seamlessly integrates with the .NET Framework, allowing you to work with a vast array of objects.

Creating Simple Objects

There are two primary ways you can create objects in PowerShell, depending on the source or type of data you want to interact with.

Using [PSCustomObject]

The [PSCustomObject] type accelerator is the most straightforward way to create custom objects in PowerShell. This is useful for creating objects to organize data from a variety of sources. [PSCustomObject] is a native PowerShell type as well, so it is generally faster and more efficient to use vs New-Object.

Here’s a basic example:

$person = [PSCustomObject]@{
Name = “John Doe”
Age  = 30
Email = “[email protected]
}

In this example, we are creating a custom object with three properties: Name, Age, and Email. This object can now be used like any other PowerShell object. We can reference individual properties of the object or use methods on it to filter, sort, or process the data efficiently.

Using New-Object

PowerShell also enables you to create objects from .NET classes using New-Object. This allows you to interact directly with .NET or COM object types that are not as easily handled by [PSCustomObject]. For example, to create a System.Diagnostics.Process object:

$process = New-Object -TypeName System.Diagnostics.Process

This creates a new Process object, which you can use to interact with system processes, including the available properties and methods that exist within it.

Accessing and Modifying Object Properties

Once a PowerShell object has been created, you can access, edit, and modify the various properties within that object.

Accessing Properties with Dot Notation

Accessing properties of an object is simple when using dot notation. Let’s look at returning a property from the Person object we created previously. We can use dot notation to reference the object property we want to return. This is simply in the format Object.Property. Here we are returning the Name property from our Person object.

Returning the Name property from a Person object.

This command retrieves the value of the Name property from the $person object, which has the value “John Doe”.

Modifying Properties

To modify the value of an object property, you can use the same dot notation to assign a new value to that specific property. Here, we will modify the Name property from “John Doe” to “Jane Doe”.

Modify the Name property from "John Doe" to "Jane Doe".

Adding and Removing Properties

In addition to being able to access and modify the properties of an object, you can also add or remove properties as needed. To add a new property to an object, use Add-Member. Let’s add a new property called “Occupation” with a value of “Developer”.

Adding a new property called "Occupation" with a value of "Developer".

Removing properties is also simple. To remove a property, use Remove-Member and specify the name of the property you wish to remove from the object:

$person | Remove-Member -Name “Occupation”

Basic Operations with Objects

The real power of PowerShell objects lies in the ability to work with the data within those objects. We can not only sort, filter, and format that data, but we can also specify what data is returned to us from the various commands used.

Selecting Object Properties

There will be instances when running commands where the result will return a very large amount of data, more than you need for your script. It’s inefficient and resource intensive to have PowerShell gather and return all that data. Instead, use the Select-Object cmdlet to filter our results to only return the necessary data.

Let’s look again at the previous example of querying Windows Services. If we simply run the Get-Service command, it will return ALL of the Windows services for the computer running that command, including all of their properties. That is a lot of data, and not all of it may be useful to us in our script.

Instead, let’s use the Select-Object cmdlet to filter out our results to only display the properties we are interested in seeing. In this case, that is the DisplayName, Status, and StartType.

Using the Select-Object cmdlet to filter out our results to only display the properties we are interested in seeing. 

Now we have a collection of objects that contain only the necessary properties for our task.

Filtering Objects

In addition to filtering out our results to only return necessary properties, we can also filter the actual query to only return objects that match our specified criteria. This is also useful for ensuring our scripts are efficient in that we are only returning the objects we intend on using instead of all objects in that category. We can use the Where-Object cmdlet to filter objects based on things like property values.

Let’s look again at our Windows Services. Instead of returning all services, let’s only return services that are currently running.

Windows Services returning all services, let's only return services that are currently running.

 

We can use the Where-Object cmdlet to specify that we only want objects returned that match our filter criteria, which in this case, is only objects with a Status property that equals “Running”.

Sorting Objects

Now that we have returned just the data we need, we can also use commands to sort our returned data. Perhaps we want to export this data to a CSV file and need it to be in a particular order, like alphabetical or by StartType. We can use the Sort-Object cmdlet to achieve this. Let’s take our previous command and add to it to sort our data by DisplayName in an ascending order.

Taking our previous command and adding to it to sort our data by DisplayName in an ascending order.

Sorting data by DisplayName in an ascending order

Using Loops on Objects

PowerShell lets us use the pipeline to process and act on a collection of objects. We can perform these actions by looping through our data. In this example, we are going to use the ForEach-Object cmdlet to loop through each of our returned objects and perform some additional data processing on it based on our query.

ForEach-Object

We can use the ForEach-Object cmdlet to perform various operations for every item within a collection, processed one object at a time via the pipeline. We simply need to pass our collection of objects to the cmdlet via the pipeline, and then specify what actions we want to perform on those objects.

If we take our previous example again, maybe we want to iterate through all of the currently running services and if the StartType is set to “Manual”, then we will run a command to set it to “Automatic”.  Another example could be that we have a list of user data, and we need to iterate through each object in the list and update a particular property with a new value or add a new property to the entry.

Here is an example of iterating through a collection of People and adding a new property called “Country” with a value of “USA”. Here we are using $_ to represent the current object in the pipeline that we are passing in to the Add-Member cmdlet.

Using $_ to represent the current object in the pipeline that we are passing in to the Add-Member cmdlet.

Looping through objects via PowerShell allows us to manipulate our data and perform actions across all of our objects effortlessly—and quickly.

Putting PowerShell Objects Best Practices in Action

Understanding and using objects in PowerShell can significantly improve the efficiency and capability of your scripts. By mastering objects, you’ll be able to effectively handle data, perform complex operations, and create powerful automation solutions.

PowerShell Automation

Looking to supercharge PowerShell processes?

LEARN MORE