Welcome to our PowerShell blog, where we delve into the world of custom objects, a powerful feature that can greatly enhance your scripting capabilities. In PowerShell, everything is an object, and sometimes the pre-defined object types don’t quite fit the bill for the data you’re working with. That’s where custom objects come into play, allowing you to create structured data that suits your specific needs.
NOTE
For more information about PowerShell objects in general please take a look at Microsoft Learn – about Objects
What are PowerShell Custom Objects?
Custom objects in PowerShell are objects defined by the user, created to hold a set of information in a structured format. They allow you to encapsulate data in an object with custom properties, making it easier to manipulate, pass, and manage data within your scripts. PowerShell mostly deals with objects that have properties. Most, if not all, output produced by PowerShell is in the form of objects of one class or another. There is so much information about PowerShell objects that I won’t be going into all aspects of them here. I’m going to focus primarily on the one object that I, and I’m certain many many others, use extensively.
When to Use Custom Objects
Custom objects are particularly useful when:
- The data you’re working with doesn’t naturally fit into an existing object type.
- You need to aggregate data from multiple sources into a single object.
- You want to ensure consistent data structure across your script or application.
Creating Custom Objects
PowerShell provides several ways to create custom objects. The most common methods are using the PSCustomObject
type accelerator or the New-Object
cmdlet.
Example: Using PSCustomObject
$userData = [PSCustomObject]@{
FirstName = "John"
LastName = "Doe"
Email = "john.doe@example.com"
}
Write-Output $userData
PowerShellThis creates a new custom object with properties for a user’s first name, last name, and email.
$allprocesses = get-process
$myobject = @()
foreach ($process in $allprocesses) {
$data = @{
Name = $process.ProcessName
Id = $process.Id
Description = $process.Description
FileVersion = $process.FileVersion
Company = $process.Company
}
$myobject += [pscustomobject]$data
}
$myobject | export-csv -Path "$($env:TEMP)\processes-selected.csv" -NoTypeInformation
PowerShellThis creates a PowerShell Custom Object ([pscustomobject]
) that are members of an array containing process information and exported to a CSV file.
Example: Using New-Object
$userData = New-Object PSObject -Property @{
FirstName = "John"
LastName = "Doe"
Email = "john.doe@example.com"
}
Write-Output $userData
PowerShellBoth methods create similar custom objects; however, [PSCustomObject]
is the recommended way in PowerShell 3.0 and above for its simplicity and performance benefits.
Manipulating Custom Objects
Once you’ve created a custom object, you can add, modify, or remove properties just as you would with any other PowerShell object.
Adding a Property
Add-Member -InputObject $userData -MemberType NoteProperty -Name "Phone" -Value "123-456-7890"
PowerShellModifying a Property
$userData.Email = "new.email@example.com"
PowerShellRemoving a Property
$userData.PSObject.Properties.Remove('Phone')
PowerShellBest Practices
- Use Descriptive Property Names: Ensure the names of the properties clearly describe the data they hold.
- Keep It Simple: Don’t add unnecessary properties; keep your custom objects lean and focused on the task.
- Reuse Objects When Possible: If you find yourself using the same custom object structure frequently, consider turning it into a function that returns the custom object.
- Document Your Objects: Especially in shared scripts, document the structure and purpose of your custom objects for clarity.
- Consistency: Be consistent with property names and object structures throughout your script to reduce complexity.
Conclusion
PowerShell custom objects are a versatile tool in any scripter’s arsenal, allowing for flexible and powerful data management. By following the examples and best practices provided in this guide, you’ll be well on your way to leveraging custom objects to streamline your PowerShell scripts, making them more maintainable and easier to understand.
Remember: Custom objects are your friend when it comes to organizing complex data. Use them wisely, and your scripts will thank you!
We hope you found this guide helpful. Stay tuned for more PowerShell tips and tricks!