Consider the following PowerShell script:
$obj = [PSCustomObject]@{Name='Alice'; Age=30; City='Seattle'}
$obj | Format-ListWhat will be the output shown in the console?
$obj = [PSCustomObject]@{Name='Alice'; Age=30; City='Seattle'}
$obj | Format-ListThink about how Format-List displays properties of an object in PowerShell.
The Format-List cmdlet displays each property on its own line with the format PropertyName : Value. So the output lists Name, Age, and City each on separate lines.
In PowerShell, when you create a PSCustomObject with properties, which of the following is true about the property types?
Think about how PowerShell handles variable types when assigning values.
PSCustomObject properties keep the data type of the value assigned to them. For example, if you assign an integer, the property is an integer; if you assign a string, it is a string.
Examine this PowerShell code snippet:
$obj = [PSCustomObject]@{Name='Bob' Age=25 City='Boston'}Why does this script produce an error?
Check the syntax rules for hashtables in PowerShell.
In PowerShell, when defining a hashtable, each key-value pair must be separated by a semicolon or newline. Missing semicolons cause a syntax error.
You have this PSCustomObject:
$person = [PSCustomObject]@{Name='Eve'; Age=28}Which command correctly adds a new property City with value "Denver" to $person?
Think about how to add properties to objects in PowerShell without recreating them.
The Add-Member cmdlet adds a new property to an existing PSCustomObject. Direct assignment like $person.City = 'Denver' does not work because PSCustomObject properties are fixed after creation.
Consider this PowerShell script:
$address = [PSCustomObject]@{Street='123 Maple St'; City='Austin'}
$person = [PSCustomObject]@{Name='John'; Age=40; Address=$address}
$person.Address.CityWhat will be the output?
$address = [PSCustomObject]@{Street='123 Maple St'; City='Austin'}
$person = [PSCustomObject]@{Name='John'; Age=40; Address=$address}
$person.Address.CityThink about how nested objects work and how to access their properties.
The $person object has a property Address which is itself a PSCustomObject. Accessing $person.Address.City returns the City property of the nested object, which is 'Austin'.