PowerShell: Creating an "empty" PSCustomObject

PowerShell: Creating an "empty" PSCustomObject

Written by Christian Ritter on Aug 10th, 2023 Views Report Post

Simplifying Creation of Empty PowerShell PSCustomObjects using Custom Functions

Creating an empty PSCustomObject in PowerShell is a common task, but the traditional approach can be verbose and repetitive. In this blog post, I'll walk you through a more structured and efficient way to achieve this using functions and property definitions.

The Traditional Approach

Frequently, developers, including myself, create empty PSCustomObjects like this:

[PSCustomObject]@{
    Name = $null
    DisplayName = $null
    Telephone = $null
    EmailAddress = $null
    Gender = $null
    Street = $null
    City = $null
}

While this method works, it can become unwieldy, especially when dealing with multiple properties. It consumes screen space and lacks a clear structure.

A Structured Solution

To address these concerns, I devised a more organized approach using a custom function New-EmptyCustomObject. This function streamlines the process of creating empty PSCustomObjects and allows for greater flexibility.

function New-EmptyCustomObject {
    param (
        [string[]]$PropertyNames
    )
    
    $customObject = [PSCustomObject]@{}
    $customObject | Select-Object -Property $PropertyNames
}

By utilizing this function, you can now create empty objects in a more organized manner:

$propertyDefinitions = @{
    Users = @(
        "FirstName", "LastName", "UserName", "Title", "Department",
        "StreetAddress", "City", "State", "PostalCode", "Country",
        "PhoneNumber", "MobilePhone", "UsageLocation", "License"
    )
    Groups = @(
        "DisplayName", "PrimarySMTP", "Description", "Owner", "Type"
    )
    JobRole = @(
        "DisplayName", "PrimarySMTP", "Description", "Type"
    )
}

$usersObject  = New-EmptyCustomObject -PropertyNames $propertyDefinitions.Users
$groupsObject = New-EmptyCustomObject -PropertyNames $propertyDefinitions.Groups
$RoleObject   = New-EmptyCustomObject -PropertyNames $propertyDefinitions.JobRole

With this approach, you effortlessly generate empty PSCustomObjects while maintaining clear property definitions. This ensures that you can easily manage and track the properties of each PSCustomObject.

Wrapping Up

In conclusion, the streamlined technique I've presented enhances the creation of empty PSCustomObjects by utilizing a custom function and well-defined property definitions. This method is not only efficient but also helps maintain a structured and organized codebase.

If you have any thoughts or feedback on this topic, feel free to share them with me on Twitter at Christian Ritter.

Best regards,

Christian.

Comments (0)