Custom PowerShell Objects

      No Comments on Custom PowerShell Objects

Ever wanted to have a custom PowerShell Object with custom property attributes? This can make your console output cleaner, and I also find it very useful when using Export-CSV.

For the purpose of this post I’ve created an object that has 3 properties: “Hostname”, “Enabled”, and “NetworkUp”. The “Hostname” property will hold the name of a computer, the “Enabled” property will tell us if its enabled in Active Directory, and the “NetworkUp” property will tell us if we can ping it by name. Here’s the output:

PSCustomObj

The script works by creating individual custom objects, and adding them to an array like so:

Cool! Want to work with 1 property in particular? You can do the following:

Or to work with individual items inside the array:

For me, the best part of using custom objects is the ability to export their data straight to CSV. Sometimes when working with custom objects and exporting to CSV you dont get the output you expect. E.G you just see type information. Using this method works seamlessly:

 

Leave a Reply

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