Whilst rarely needed when running PowerShell commands in the console, creating custom objects can be a useful technique when writing scripts or functions. The typical use of custom objects is where you want to retrieve information from multiple sources and combine it for manipulation or display purposes.
There are several different ways that we can go about creating custom objects. These include:
- Abusing an existing object
- Creating a PSCustom object
- Creating a PSCustom object from a hash table
- Build your own .NET class (PowerShell 5.0)
Abusing an Existing Object
The –Property parameter of Select-Object will allow you select the properties of an object to be used subsequently in the pipeline. One use of this is to return fewer properties than were previously used, but if you specify a name of a property that does not exist, Select-Object will actually add a property with that name to the object. String objects have few existing properties, so are a good choice for creating a custom object, an empty string probably being the best bet. Continue reading Creating PowerShell Custom Objects