#17 PowerShell Learning: Everything about PSCustomObject

Table of Contents

PSCustomObject is a great tool to add into your PowerShell tool belt. Let's start with the basics and work our way into the more advanced features. The idea behind using a PSCustomObject is to have a simple way to create structured data. Take a look at the first example and you'll have a better idea of what that means.

Creating a PSCustomObject

I love using [PSCustomObject] in PowerShell. Creating a usable object has never been easier. Because of that, I’m going to skip over all the other ways you can create an object, but I need to mention that most of these examples are PowerShell v3.0 and newer.

$myObject = [PSCustomObject]@{
    Name     = 'Kevin'
    Language = 'PowerShell'
    State    = 'Texas'
}

This method works well for me because I use hashtables for just about everything. But there are times when I would like PowerShell to treat hashtables more like an object. The first place you notice the difference is when you want to use Format-Table or Export-CSV and you realize that a hashtable is just a collection of key/value pairs.

You can then access and use the values like you would a normal object.

PS C:\> $myObject.Name
Kevin

Converting a hashtable

While I am on the topic, did you know you could do this:

$myHashtable = @{
    Name     = 'Kevin'
    Language = 'PowerShell'
    State    = 'Texas'
}
$myObject = [pscustomobject]$myHashtable
PS C:\> $myObject

Name  Language   State
----  --------   -----
Kevin PowerShell Texas

I do prefer to create the object from the start but there are times you have to work with a hashtable first. This example works because the constructor takes a hashtable for the object properties. One important note is that while this method works, it isn’t an exact equivalent. The biggest difference is that the order of the properties isn’t preserved.

By default, hashtables aren’t ordered (or sorted). In the traditional context, the order doesn’t matter when you always use a key to access values. You may find that you want the properties to stay in the order that you define them. Thankfully, there’s a way to do that with the ordered keyword.

$person = [ordered]@{
    name = 'Kevin'
    age  = 36
}

You may have seen people use New-Object to create custom objects. This way is quite a bit slower, but it may be your best option on early versions of PowerShell.

$myHashtable = @{
    Name     = 'Kevin'
    Language = 'PowerShell'
    State    = 'Texas'
}

$myObject = New-Object -TypeName PSObject -Property $myHashtable

Saving to a file

I find the best way to save a hashtable to a file is to save it as JSON. You can import it back into a [PSCustomObject]

$myObject | ConvertTo-Json -depth 1 | Set-Content -Path $Path
$myObject = Get-Content -Path $Path | ConvertFrom-Json

Working with properties

Adding properties

You can still add new properties to your PSCustomObject with Add-Member.

PS C:\> $myObject | Add-Member -MemberType NoteProperty -Name 'ID' -Value 'KevinMarquette'

PS C:\> $myObject

Name  Language   State ID
----  --------   ----- --
Kevin PowerShell Texas KevinMarquette

PS C:\> $myObject.ID
KevinMarquette

Remove properties

You can also remove properties off of an object. The .psobject is an intrinsic member that gives you access to base object metadata.

PS C:\> $myObject.psobject.properties.remove('ID')
PS C:\> $myObject

Name  Language   State
----  --------   -----
Kevin PowerShell Texas

PS C:\> $myObject.ID
PS C:\>

Enumerating property names

Sometimes you need a list of all the property names on an object.

PS C:\> $myObject | Get-Member -MemberType NoteProperty | Select -ExpandProperty Name
Language
Name
State

PS C:\> $myobject.psobject.properties.name
Name
Language
State

Dynamically accessing properties

I already mentioned that you can access property values directly. Or you can use a string for the property name, and it will still work. Or we can take this one more step and use a variable for the property name.

PS C:\> $myObject.Name
Kevin

PS C:\> $myObject.'Name'
Kevin

PS C:\> $property = 'Name'
PS C:\> $myObject.$property
Kevin

Convert PSCustomObject into a hashtable

To continue on from the last section, you can dynamically walk the properties and create a hashtable from them.

PS C:\> $hashtable = @{}
PS C:\> foreach( $property in $myobject.psobject.properties.name )
>> {
>>     $hashtable[$property] = $myObject.$property
>> }

PS C:\> $hashtable

Name                           Value
----                           -----
Name                           Kevin
Language                       PowerShell
State                          Texas

Closing words

The context of this was all about [PSCustomObject], but a lot of this information applies to objects in general.

I have seen most of these features in passing before but never saw them presented as a collection of information on PSCustomObject. Just this last week I stumbled upon another one and was surprised that I had not seen it before. I wanted to pull all these ideas together so you can hopefully see the bigger picture and be aware of them when you have an opportunity to use them. I hope you learned something and can find a way to work this into your scripts.

#Learning PowerShell (Articles in series)Cmdlets
#1Getting Started with Windows PowerShell
#2Working with Windows PowerShell
#3Writing and Running PowerShell Scripts
#4Windows PowerShell Remoting
#5Windows PowerShell Profiles
#6PowerShell Drives and Providers
#7Windows PowerShell ISE
#8Manage Services using PowerShell
#9Manage Processes using PowerShellGet-Member, -Property, -ExpandProperty
#10Variables in Windows PowerShell
#11Arrays in Windows PowerShell
#12Hash Tables in Windows PowerShell
#13PowerShell Loopsforeach, for
#14Conditional Logic in PowerShell
#15PowerShell Functions
#16One-liners and the Pipeline
#17Everything about PSCustomObject
#18Selecting Parts of Objects
#19Sorting Objects
#20Getting WMI objects with Get-CimInstance
#21Parentheses, Braces and Square Brackets() {} []
#22The Philosophy Verb-NounGet-Command, Verb-Noun
#23PowerShell Parameters$
#24PowerShell and WMIGet-CimInstance

Leave a Comment

Required fields are marked *