PowerShell on macOS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When running PowerShell scripts on macOS, it's important to understand how the script's running time changes as the input grows.
We want to see how the script's work increases when we give it more data.
Analyze the time complexity of the following code snippet.
# List all files in a directory and print their names
$files = Get-ChildItem -Path "." -File
foreach ($file in $files) {
Write-Output $file.Name
}
This script gets all files in the current folder and prints each file name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each file in the list.
- How many times: Once for every file found in the directory.
As the number of files increases, the script prints more names, so it does more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 print actions |
| 100 | About 100 print actions |
| 1000 | About 1000 print actions |
Pattern observation: The work grows directly with the number of files.
Time Complexity: O(n)
This means the script's running time grows in a straight line as the number of files increases.
[X] Wrong: "The script runs in the same time no matter how many files there are."
[OK] Correct: Each file adds one more print action, so more files mean more work and longer running time.
Understanding how loops affect script speed helps you write efficient scripts and explain your code clearly in real situations.
"What if we added a nested loop to compare each file with every other file? How would the time complexity change?"
Practice
Solution
Step 1: Recall the PowerShell command on macOS
On macOS, PowerShell is started using thepwshcommand, notpowershellwhich is used on Windows.Step 2: Identify the correct command
Among the options, onlypwshis the correct command to launch PowerShell on macOS.Final Answer:
pwsh -> Option AQuick Check:
PowerShell on macOS starts with pwsh [OK]
- Typing 'powershell' instead of 'pwsh' on macOS
- Using 'ps' which lists processes, not PowerShell
- Assuming 'shell' starts PowerShell
script.ps1 on macOS terminal?Solution
Step 1: Understand how to run scripts in PowerShell on macOS
On macOS, you run PowerShell scripts by callingpwshfollowed by the script name.Step 2: Check each option
./script.ps1tries to run the script directly, which may fail without execution permission and PowerShell context.powershell -file script.ps1uses Windows syntax.run script.ps1is invalid.pwsh script.ps1correctly runs the script with PowerShell.Final Answer:
pwsh script.ps1 -> Option CQuick Check:
Run scripts with 'pwsh script.ps1' on macOS [OK]
- Trying to run script directly without 'pwsh'
- Using Windows PowerShell syntax on macOS
- Using 'run' command which doesn't exist
pwsh -Command "Write-Output 'Hello macOS'"
Solution
Step 1: Understand the command structure
The command usespwsh -Commandto run a PowerShell command inline, which outputs the string 'Hello macOS'.Step 2: Predict the output
TheWrite-Outputcmdlet prints the string to the terminal, so the output will be exactly 'Hello macOS'.Final Answer:
Hello macOS -> Option AQuick Check:
Write-Output prints text to terminal [OK]
- Confusing command string with output
- Assuming 'pwsh' is not installed
- Expecting error due to quotes
pwsh ./myscript.ps1 but get a permission denied error. What is the most likely fix?Solution
Step 1: Identify cause of permission denied
On macOS, scripts need execute permission to run. Without it, you get a permission denied error.Step 2: Fix permission issue
Usingchmod +x myscript.ps1adds execute permission, allowing the script to run.Final Answer:
Run chmod +x myscript.ps1 to add execute permission -> Option BQuick Check:
Permission denied? Add execute permission with chmod [OK]
- Trying to run script without execute permission
- Renaming script to .sh which doesn't help PowerShell
- Using sudo unnecessarily
- Reinstalling PowerShell instead of fixing permissions
files.txt. Which command correctly does this?Solution
Step 1: Use PowerShell cmdlet to list files
Get-ChildItemlists files and folders in PowerShell. Using~/Documentstargets the Documents folder.Step 2: Redirect output to a file in PowerShell
PowerShell usesOut-Fileto save output to a file. The command runs insidepwsh -Commandto execute from macOS terminal.Final Answer:
pwsh -Command "Get-ChildItem ~/Documents | Out-File files.txt" -> Option DQuick Check:
Use Get-ChildItem with Out-File inside pwsh [OK]
- Using shell redirection > inside PowerShell command incorrectly
- Using 'ls' which is not a PowerShell cmdlet
- Running PowerShell cmdlets outside pwsh context
- Omitting Out-File to save output
