Introduction
Custom objects let you group related information together in one place. They make data easier to organize and use.
Custom objects let you group related information together in one place. They make data easier to organize and use.
@{ Property1 = 'Value1'; Property2 = 'Value2' } | ForEach-Object { [PSCustomObject]$_ }$person = @{ Name = 'Alice'; Age = 30 } | ForEach-Object { [PSCustomObject]$_ }
$person$car = [PSCustomObject]@{ Make = 'Toyota'; Model = 'Corolla'; Year = 2020 }
$car$book = [PSCustomObject]@{ Title = 'PowerShell Guide'; Pages = 250 }
$book.TitleThis script creates a custom object for a student with three properties. Then it prints each property clearly.
$student = [PSCustomObject]@{
Name = 'John Doe'
Grade = 'A'
Age = 16
}
Write-Output "Student Info:"
Write-Output "Name: $($student.Name)"
Write-Output "Grade: $($student.Grade)"
Write-Output "Age: $($student.Age)"You can add as many properties as you want to a PSCustomObject.
Custom objects are great for organizing data in scripts and making output easier to read.
Use dot notation to get or set property values.
Custom objects group related data with named properties.
Use @{ } with [PSCustomObject] to create them easily.
They help organize and display data clearly in PowerShell scripts.
[PSCustomObject] in PowerShell?[PSCustomObject] does[PSCustomObject] creates an object with named properties to hold related data clearly.[PSCustomObject] = Organize data [OK][PSCustomObject] groups data with names [OK]Name and Age in PowerShell?[PSCustomObject]@{PropertyName='Value'; PropertyName2=Value2} with semicolons separating properties.$person = [PSCustomObject]@{Name='Bob'; Age=25}; $person.Name$person holds a custom object with properties Name and Age. Accessing $person.Name returns the value of the Name property.Name property was set to 'Bob', so the output is 'Bob'.$obj = [PSCustomObject]@{Name='Eve' Age=28}Name='Eve' and Age=28 are missing a semicolon between them.PSCustomObject are correct. Property names do not require quotes. Numbers can be property values.Title and Author. Which PowerShell code correctly creates an array of two such objects?[PSCustomObject]. To create an array, wrap the objects in @( ) and separate them with commas.Pass the test and this pattern moves unknown