Using PSCustomObject in PowerShell

This is one of the most used tools in PowerShell that I use, an excellent way of storing and using data for reporting purposes. In this blog post, I will show you how to use it to your benefit.

What is PSCustomObject?

It’s like a structured table that consists of properties and values, and it has been there since PowerShell version 3.0. The TypeName is PSCustomObject in the System.Management.Automation category.

The legacy way of creating a PSCustomObject

Instead of using the newer and faster PSCustomObject, you could create a new object first and add Members. For example:


This still works but is considered legacy and slower, so the way to go is to use a PSCustomObject.

How to create a PSCustomObject

Creating a new PSCustomobject is easy, especially in Visual Studio Code 🙂 You can start typing ‘psc’ and hit the TAB key to auto-complete it:

It will expand to this below, which is the start of a PSCustomObject:

Using a simple PSCustomObject

Now that the syntax is there, you can start filling it. In many of my scripts, I use this to model data that can be exported to a .csv file. In the example below, I use it to fill a $total variable with details that I gathered from a Get-Service query. I named the Properties, like Name/DisplayName, just like Get-Service returns them. With a PSCustomObject, you can name them however you like.

The output will look like this:

Using an advanced PSCustomObject

You can also use conditions in the values in a PSCustomOject. Depending on the returned data, you can use that value or change it to something if no data was found. In the example below, I reused the Get-Service one but with a change to the DependentServices property. If no data were found for that, it would display nothing. I changed it to check for that and show “No dependent service found” if no dependent service was found.

Now the output looks like this:

More information

For more information about using PSCustomObject, you can check these links:


2 thoughts on “Using PSCustomObject in PowerShell

  1. Nice, thanks. I may not use this exact script daily but it is is a very good example and the method is easy to follow. I have a use case for the If-Else in the customobject properties right now on my current project.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.