top of page

PowerShell Custom Objects

Combine Data with PSObject & PSCustomObject

PowerShell custom objects are a powerful feature that allow you to create objects with properties and methods tailored to your specific needs. These custom objects can be used to structure and manipulate data in a more intuitive and readable way compared to using raw data structures like arrays or hashtables. Here's a brief overview of PowerShell custom objects:

Advantages of Custom Objects

  1. Clarity and Readability: Custom objects make your scripts more readable and maintainable by giving structure to your data.

  2. Ease of Use: They integrate well with PowerShell cmdlets and pipeline operations.

  3. Flexibility: You can dynamically add or remove properties and methods as needed.

​

Use Cases

  • Data Processing: Structuring and managing data from various sources (e.g., CSV files, APIs, databases).

  • Configuration Management: Representing configuration settings or state information in a clear and structured way.

  • Automation Scripts: Enhancing the functionality and clarity of automation scripts by encapsulating related data and methods.

​

Custom objects in PowerShell are an essential tool for scripting and automation, enabling the creation of complex, readable, and maintainable scripts.

Custom PowerShell Object: (Legacy)

​

$Obj=New-Object PSObject 
$Obj |Add-Member NoteProperty MyName ("Scott Head")
$Obj |Add-Member NoteProperty MyHome ("Redondo")
$Obj |Add-Member NoteProperty MyCar ("Mustang")
$Obj |Add-Member NoteProperty MyJob ("Chevron Oil")

Return $Obj 

New Custom PowerShell Object Format:
         

#Declare Array For Storing Multiple Object Data
$MyObjectArray=@()

#Create, Populate Object Data and Assign
$MyObjectVariable=[PSCustomObject]@{
FirstName = "SCOTT"         
LastName = "HEAD"
Location="Redondo"        
Job="Sr. Systems Admin"        
SomeNUmber=455   
}  

Combine Data with New-Object PSObject PS Ver. 1

# Use WMI get IP on My Subnet
$IPAddress = Get-WmiObject Win32_NetworkAdapterConfiguration | Where{$_.IPAddress -Like "192.168.254*"} | Select -ExpandProperty IPAddress

# Use WMI get Operating System
$OS = (Get-WmiObject Win32_Operatingsystem ).Version

# Get-Host PowerShell Version
$PSVersion= (Get-Host | Select -ExpandProperty Version).major

#Build Object & Assign Values
$Obj=New-Object PSObject 
$Obj |Add-Member NoteProperty 'Server Name' ($Env:Computername)
$Obj |Add-Member NoteProperty 'Operating System' ($OS)
$Obj |Add-Member NoteProperty 'PowerShell Version' ($PSVersion[0])
$Obj |Add-Member NoteProperty 'IPv4 Address' ($IPAddress)
cls
$Obj  | Format-list 

Results-PSObject.JPG

A way to combine data from data different sources into one Object that can also be added to an Array.

Combine Data with PSCustom Object PS Ver. 3

# Use WMI get IP on My Subnet
$IPAddress = Get-WmiObject Win32_NetworkAdapterConfiguration | Where{$_.IPAddress -Like "192.168.254*"} | Select -ExpandProperty IPAddress
# Use WMI get Operating System
$OS = (Get-WmiObject Win32_Operatingsystem ).Version
# Get-Host PowerShell Version
$PSVersion= (Get-Host | Select -ExpandProperty Version).major
#Build Object & Assign Values
$MyObject = [PSCustomObject]@{
    'Server Name'       = ($Env:Computername)
    'Operating System'  = ($OS)
    'PowerShell Version'= ($PSVersion[0])
    'IPv4 Address'      = ($IPAddress)
}
$MyObject | Format-List

Results-PSObject.JPG
bottom of page