How to create a PSCustomObject

As I wrote in a previous blog post, everything in PowerShell are objects.

There is also a custom PowerShell object with the type PSCustomObject.

This post is just gonna be a quick and dirty “How to”. If you want to read the docs, you’ll find them here.

Wraps an object providing alternate views of the available members and ways to extend them. Members can be methods, properties, parameterized properties, etc.

There are several ways of creating a PSCustomObject.

You can create it using New-Object and specifying PSCustomObject as the type:

 $myObject = New-Object -Typename PSCustomObject 

c1

You then add properties and data like shown above.

Another way of doing it is by declaring it as a [PSCustomObject] like this:

c2

Now calling $myObject will show you your new object. And running Get-Member will show you that it’s of the type System.Management.Automation.PSCustomObject.

c3

That’s it! 🙂

Quick and dirty post, I’ll try to think of some good topics to do longer write-ups on later. This can be a good and quick reference page if you forget how to construct your custom objects!

// Tr4p ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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