Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

#17 PowerShell Learning: Everything about PSCustomObject

August 14, 2023
in Blog, PowerShell Learning
0
ADVERTISEMENT

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.

5/5 - (1 vote)
Previous Post

#16 PowerShell Learning: One-liners and the Pipeline

Next Post

#18 PowerShell Learning: Selecting Parts of Objects

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory