PowerShell vs Bash vs CMD comparison - Performance Comparison
When comparing PowerShell, Bash, and CMD scripts, it's important to see how their execution time grows as the input size increases.
We want to understand which shell handles repeated tasks faster as the workload grows.
Analyze the time complexity of a simple loop that prints numbers from 1 to n in each shell.
# PowerShell example
for ($i = 1; $i -le $n; $i++) {
Write-Output $i
}
# Bash example
for i in $(seq 1 $n); do
echo $i
done
# CMD example
for /L %i in (1,1,%n%) do (
echo %i
)
This code prints numbers from 1 to n using a loop in each shell.
Identify the loops that repeat the print operation.
- Primary operation: Looping from 1 to n and printing each number.
- How many times: Exactly n times, once per number.
As n grows, the number of print operations grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print commands |
| 100 | 100 print commands |
| 1000 | 1000 print commands |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to complete the task grows in direct proportion to the number of items printed.
[X] Wrong: "PowerShell, Bash, or CMD loops run in constant time no matter how many items are printed."
[OK] Correct: Each loop iteration does work, so more items mean more time. The time grows with n, not fixed.
Understanding how loops scale in different shells helps you write efficient scripts and explain your choices clearly in real-world tasks.
What if we nested two loops each running from 1 to n? How would the time complexity change?
Practice
Solution
Step 1: Understand shell types
PowerShell is known for handling objects, unlike Bash and CMD which handle text.Step 2: Identify platform focus
PowerShell is designed mainly for Windows automation, while Bash is common on Linux/macOS and CMD is basic Windows shell.Final Answer:
PowerShell -> Option CQuick Check:
Object-based Windows shell = PowerShell [OK]
- Confusing Bash as object-based
- Thinking CMD is object-based
- Selecting shells not related to Windows
Solution
Step 1: Recall PowerShell commands
PowerShell uses cmdlets like Get-ChildItem to list files.Step 2: Compare with other shells
ls -l is Bash/Linux syntax, dir /w is CMD syntax, listfiles is invalid.Final Answer:
Get-ChildItem -> Option DQuick Check:
PowerShell file list = Get-ChildItem [OK]
- Using Bash command ls -l in PowerShell
- Using CMD dir /w in PowerShell
- Typing invalid commands like listfiles
Get-Process | Where-Object { $_.CPU -gt 100 }Solution
Step 1: Understand Get-Process output
Get-Process returns process objects with a CPU property showing CPU time in seconds.Step 2: Analyze Where-Object filter
The filter $_.CPU -gt 100 selects processes with CPU time greater than 100 seconds.Final Answer:
Lists all processes with CPU usage greater than 100 seconds -> Option BQuick Check:
Filter CPU > 100 seconds = Lists all processes with CPU usage greater than 100 seconds [OK]
- Confusing greater than (gt) with less than
- Assuming CPU property is missing
- Thinking it lists all processes without filtering
ls -l | grep ".txt"
Solution
Step 1: Check command compatibility
PowerShell supports ls alias but not with -l option; it uses Get-ChildItem instead.Step 2: Identify grep usage
grep is a Linux command, not native in PowerShell; PowerShell uses Where-Object or Select-String.Final Answer:
PowerShell does not support ls -l syntax -> Option AQuick Check:
ls -l syntax unsupported = PowerShell does not support ls -l syntax [OK]
- Assuming ls -l works fully in PowerShell
- Thinking pipe | is invalid
- Believing quote style causes error here
Solution
Step 1: Consider cross-platform support
PowerShell Core (PowerShell 7+) runs on Windows and Linux, supporting Get-ChildItem on both.Step 2: Evaluate other options
Bash scripts work only on Linux/macOS; CMD batch files only on Windows; separate scripts add complexity.Final Answer:
Write a PowerShell script using Get-ChildItem and run PowerShell Core on both OS -> Option AQuick Check:
Cross-platform scripting = PowerShell Core + Get-ChildItem [OK]
- Assuming Bash works on Windows by default
- Thinking CMD scripts run on Linux
- Ignoring cross-platform PowerShell Core availability
Ready to prove it?
Pass the test and this pattern moves unknown
