0
0
PowerShellscripting~5 mins

Custom objects (PSCustomObject) in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a PSCustomObject in PowerShell?
A PSCustomObject is a way to create custom objects with properties and values in PowerShell. It helps organize data like a simple table or record.
Click to reveal answer
beginner
How do you create a PSCustomObject with properties Name and Age?
Use the syntax: <br>$obj = [PSCustomObject]@{Name='Alice'; Age=30}<br>This creates an object with two properties: Name and Age.
Click to reveal answer
intermediate
Why use PSCustomObject instead of a hashtable?
PSCustomObject lets you create objects that behave like real objects with properties, making it easier to work with in scripts and output. Hashtables are just key-value pairs without object behavior.
Click to reveal answer
intermediate
How can you add a new property to an existing PSCustomObject?
You can add a property by using Add-Member cmdlet, for example:<br>$obj | Add-Member -NotePropertyName 'City' -NotePropertyValue 'Seattle'
Click to reveal answer
beginner
What will this script output?<br>$person = [PSCustomObject]@{Name='Bob'; Age=25}<br>$person.Age = 26<br>$person
It outputs the object with updated Age property:<br>Name Age<br>---- ---<br>Bob 26<br>This shows you can change property values after creation.
Click to reveal answer
Which syntax correctly creates a PSCustomObject with a property 'Color' set to 'Red'?
A$obj = @{Color='Red'}
B$obj = [Object]@{Color='Red'}
C$obj = New-Object Color='Red'
D$obj = [PSCustomObject]@{Color='Red'}
How do you add a new property 'Country' with value 'USA' to an existing PSCustomObject $obj?
A$obj.Country = 'USA'
B$obj.Add('Country','USA')
C$obj | Add-Member -NotePropertyName 'Country' -NotePropertyValue 'USA'
DYou cannot add properties after creation
What type of data structure is a PSCustomObject in PowerShell?
AA custom object with properties
BA key-value pair hashtable
CA simple string
DAn array of values
Which command updates the 'Age' property of a PSCustomObject $person to 40?
A$person = @{Age=40}
B$person.Age = 40
C$person.Add('Age',40)
D$person | Set-Member -Name Age -Value 40
What happens if you try to create a PSCustomObject without using [PSCustomObject] type accelerator?
AYou get an empty object
BPowerShell throws an error
CYou get a hashtable, not an object
DIt creates a PSCustomObject anyway
Explain how to create a PSCustomObject with three properties and how to access one of those properties.
Think about how you write a hashtable and then convert it to an object.
You got /3 concepts.
    Describe how to add a new property to an existing PSCustomObject and why this might be useful.
    Remember you can modify objects after creation with a special cmdlet.
    You got /3 concepts.