Bird
0
0

How can you create a custom object in PowerShell with properties 'Name' and 'Age'?

hard📝 Application Q9 of 15
PowerShell - Working with Objects
How can you create a custom object in PowerShell with properties 'Name' and 'Age'?
A$person = New-Object Name, Age
B$person = [PSCustomObject]@{Name='Alice'; Age=30}
C$person = @{Name='Alice'; Age=30}
D$person = Object(Name='Alice', Age=30)
Step-by-Step Solution
Solution:
  1. Step 1: Understand custom object creation

    Use [PSCustomObject] with a hashtable to create objects with properties.
  2. Step 2: Check syntax correctness

    $person = [PSCustomObject]@{Name='Alice'; Age=30} correctly uses [PSCustomObject] and a hashtable with Name and Age.
  3. Final Answer:

    $person = [PSCustomObject]@{Name='Alice'; Age=30} -> Option B
  4. Quick Check:

    Custom object creation = A [OK]
Quick Trick: Use [PSCustomObject] with @{ } to create objects with properties [OK]
Common Mistakes:
  • Using New-Object incorrectly
  • Confusing hashtables with objects
  • Using invalid syntax for object creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes