Of course! Creating a custom object, or PSObject, is a fundamental task in PowerShell. It allows you to group related data into a single, structured variable.
The modern and highly recommended way to create a custom object is by using the [PSCustomObject] type accelerator. It's clean, readable, and efficient.
The Best Method: [PSCustomObject] (PowerShell 3.0+)
This is the standard, modern approach. You define the object's properties and values inside a hashtable and then "cast" or "convert" that hashtable into a PSCustomObject.
How it Works
You create a hashtable (@{...}) where each key becomes a property name and its corresponding value becomes the property's value.
Example
Let's create an object representing a computer system.
powershell
# Define the object using a hashtable and casting it to [PSCustomObject]
$systemInfo = [PSCustomObject]@{
ComputerName = "SRV-WEB-01"
IPAddress = "192.168.1.10"
OS = "Windows Server 2022"
RAM_GB = 32
InProduction = $true
}
# Now you can interact with the object
# Display the whole object
Write-Host "--- Displaying the full object ---"
$systemInfo
# Access a specific property using dot notation
Write-Host "`n--- Accessing a single property ---"
Write-Host "The IP Address is: $($systemInfo.IPAddress)"
# Use the object in a string
Write-Host "`n--- Using the object in a sentence ---"
$status = if ($systemInfo.InProduction) { "is" } else { "is not" }
Write-Host "$($systemInfo.ComputerName) runs $($systemInfo.OS) and $status in production."
Output of the Example:
code
--- Displaying the full object ---
ComputerName : SRV-WEB-01
IPAddress : 192.168.1.10
OS : Windows Server 2022
RAM_GB : 32
InProduction : True
--- Accessing a single property ---
The IP Address is: 192.168.1.10
--- Using the object in a sentence ---
SRV-WEB-01 runs Windows Server 2022 and is in production.
Key Advantages of this method:
Readability: The syntax is very clear and self-explanatory.
Performance: It's the fastest way to create a structured object.
Ordered Properties: The properties on the object will appear in the same order you defined them in the hashtable. This is a huge benefit for creating predictable output (e.g., for CSV files).
Older / Alternative Methods
You will encounter other methods in older scripts or online examples. It's good to know what they are, even if you don't use them for new code.
1. New-Object with -Property
This was the common method in PowerShell v2. It also uses a hashtable.
powershell
$systemInfo_old = New-Object -TypeName PSObject -Property @{
ComputerName = "SRV-DB-01"
IPAddress = "192.168.1.15"
OS = "Windows Server 2019"
}
# The object works the same way
$systemInfo_old.ComputerName
Why it's not preferred: In PowerShell v2, the property order was not guaranteed. While this was fixed in later versions, the [PSCustomObject] syntax is more concise and is the accepted modern standard.
2. New-Object and Add-Member
This method is the most verbose. You create an empty object first and then add properties to it one by one.
powershell
# 1. Create an empty object
$systemInfo_verbose = New-Object -TypeName PSObject
# 2. Add properties to it one by one
$systemInfo_verbose | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value "SRV-FS-01"
$systemInfo_verbose | Add-Member -MemberType NoteProperty -Name "IPAddress" -Value "192.168.1.20"
$systemInfo_verbose | Add-Member -MemberType NoteProperty -Name "OS" -Value "Windows Server 2022"
# The object works the same way
$systemInfo_verbose.OS
When to use this: This method is still useful in specific scenarios, like when you need to add properties to an object conditionally inside a loop or a complex function. For simple object creation, it's too much work.
Summary: Which One Should You Use?
Method
When to Use
Pros
Cons
[PSCustomObject]
Almost always. This is the modern best practice for new scripts.
Clean, fast, readable, preserves property order.
Requires PowerShell 3.0+ (which is standard everywhere now).
New-Object -Property
When you must maintain compatibility with PowerShell v2.
Works in very old environments.
Verbose, property order not guaranteed in PSv2.
Add-Member
When you need to add properties dynamically or conditionally to an existing object.