Custom objects (PSCustomObject) in PowerShell - Time & Space Complexity
When creating custom objects in PowerShell, it is helpful to understand how the time to build these objects grows as you add more data.
We want to know how the work changes when making many custom objects.
Analyze the time complexity of the following code snippet.
$results = @()
for ($i = 1; $i -le $n; $i++) {
$obj = [PSCustomObject]@{
Id = $i
Name = "Item$i"
}
$results += $obj
}
$results
This code creates $n custom objects, each with two properties, and adds them to an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new custom object (O(1)) and adding it to the array with += (O(current size)).
- How many times: n times, but total cost is O(n2).
As you increase n, the number of objects created and added grows directly with n.
| Input Size (n) | Approx. Total Operations |
|---|---|
| 10 | 55 (10 creations + 45 copies) |
| 100 | 5,050 (100 creations + 4,950 copies) |
| 1000 | 500,500 (1,000 creations + 499,500 copies) |
Pattern observation: The work grows quadratically as n grows; doubling n roughly quadruples the work.
Time Complexity: O(n^2)
This means the time to create and add objects grows quadratically with the number of objects.
[X] Wrong: "Adding objects to the array is always fast and constant time."
[OK] Correct: In PowerShell, adding to an array with += creates a new array each time, so the time grows more than expected as the array gets bigger.
Understanding how loops and object creation scale helps you write scripts that stay fast even with lots of data. This skill shows you can think about efficiency in real tasks.
What if we used a list object instead of an array and added items with Add()? How would the time complexity change?
Practice
[PSCustomObject] in PowerShell?Solution
Step 1: Understand what
[PSCustomObject]does[PSCustomObject]creates an object with named properties to hold related data clearly.Step 2: Compare options
Only "To create objects with named properties for organizing related data" correctly describes this purpose. Other options describe unrelated tasks.Final Answer:
To create objects with named properties for organizing related data -> Option BQuick Check:
Purpose of[PSCustomObject]= Organize data [OK]
[PSCustomObject] groups data with names [OK]- Thinking it speeds up command execution
- Confusing with arrays or type conversion
- Assuming it creates numeric sequences
Name and Age in PowerShell?Solution
Step 1: Recall correct syntax for custom object creation
The correct syntax uses[PSCustomObject]@{PropertyName='Value'; PropertyName2=Value2}with semicolons separating properties.Step 2: Analyze each option
[PSCustomObject]@{Name='Alice'; Age=30} matches the correct syntax. @{Name='Alice', Age=30} [PSCustomObject] reverses order. [PSCustomObject] @{Name: 'Alice'; Age: 30} uses colons instead of equals. [PSCustomObject]@{Name='Alice' Age=30} misses semicolon between properties.Final Answer:
[PSCustomObject]@{Name='Alice'; Age=30} -> Option CQuick Check:
Correct syntax uses semicolons and equals [OK]
- Using commas instead of semicolons
- Placing [PSCustomObject] after the hashtable
- Using colons instead of equals signs
$person = [PSCustomObject]@{Name='Bob'; Age=25}; $person.NameSolution
Step 1: Understand object creation and property access
The variable$personholds a custom object with propertiesNameandAge. Accessing$person.Namereturns the value of theNameproperty.Step 2: Determine the output
TheNameproperty was set to 'Bob', so the output is 'Bob'.Final Answer:
Bob -> Option AQuick Check:
Access property returns its value [OK]
- Expecting the whole object printed
- Confusing property name with value
- Using wrong property name
$obj = [PSCustomObject]@{Name='Eve' Age=28}Solution
Step 1: Check hashtable property separators
Properties inside the hashtable must be separated by semicolons. Here,Name='Eve'andAge=28are missing a semicolon between them.Step 2: Verify other syntax elements
Square brackets aroundPSCustomObjectare correct. Property names do not require quotes. Numbers can be property values.Final Answer:
Missing semicolon between properties inside the hashtable -> Option AQuick Check:
Separate properties with semicolons [OK]
- Using commas instead of semicolons
- Adding quotes around property names unnecessarily
- Thinking numbers can't be property values
Title and Author. Which PowerShell code correctly creates an array of two such objects?Solution
Step 1: Understand how to create multiple custom objects in an array
Each book must be a separate[PSCustomObject]. To create an array, wrap the objects in@( )and separate them with commas.Step 2: Analyze each option
@([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) correctly creates two separate custom objects inside an array. [PSCustomObject]@{Title='1984', Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'} uses comma instead of semicolon in the first hashtable, causing a syntax error. @{Title='1984'; Author='Orwell'}, @{Title='Dune'; Author='Herbert'} creates hashtables, not custom objects. [PSCustomObject]@{Title='1984', Author='Orwell'; Title='Dune', Author='Herbert'} tries to combine two books in one object incorrectly.Final Answer:
@([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) -> Option DQuick Check:
Use array @( ) with separate custom objects [OK]
- Using commas instead of semicolons inside hashtables
- Using hashtables instead of custom objects
- Combining multiple items in one object incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
