Bulk user operations from CSV in PowerShell - Time & Space Complexity
When running bulk user operations from a CSV file, it is important to understand how the time to complete the task grows as the number of users increases.
We want to know how the script's work changes when the list of users gets bigger.
Analyze the time complexity of the following code snippet.
Import-Csv -Path 'users.csv' | ForEach-Object {
# Example operation: Create user
New-ADUser -Name $_.Name -SamAccountName $_.Username
}
This script reads a CSV file of users and creates a new user account for each entry.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
ForEach-Objectloop that processes each user from the CSV. - How many times: Once for every user in the CSV file.
Each user in the CSV causes one operation to run, so the total work grows directly with the number of users.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 user creations |
| 100 | 100 user creations |
| 1000 | 1000 user creations |
Pattern observation: Doubling the number of users doubles the work needed.
Time Complexity: O(n)
This means the time to complete the script grows in a straight line as the number of users increases.
[X] Wrong: "The script runs in the same time no matter how many users are in the CSV."
[OK] Correct: Each user requires a separate operation, so more users mean more work and more time.
Understanding how loops over input data affect time helps you explain script performance clearly and shows you can reason about scaling tasks.
"What if the script also checked each user against an existing database before creating? How would the time complexity change?"