Bird
Raised Fist0
PowerShellscripting~20 mins

PowerShell on macOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
PowerShell macOS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell command on macOS?
Consider this command run in PowerShell on macOS:
Get-ChildItem -Path ~ -File | Where-Object { $_.Length -gt 1000 } | Select-Object -First 1 -Property Name, Length

What does this command output?
PowerShell
Get-ChildItem -Path ~ -File | Where-Object { $_.Length -gt 1000 } | Select-Object -First 1 -Property Name, Length
AAn empty list because no files are returned
BAn error saying 'Where-Object' is not recognized
CAn object showing the Name and Length of the first file in the home directory larger than 1000 bytes
DA list of all files in the home directory regardless of size
Attempts:
2 left
💡 Hint
Think about filtering files by size and selecting only the first match.
📝 Syntax
intermediate
1:30remaining
Which PowerShell syntax correctly sets an environment variable on macOS?
You want to set an environment variable named MY_VAR with value 'hello' in PowerShell on macOS. Which option is correct?
Aenv MY_VAR='hello'
B$env:MY_VAR = 'hello'
Cexport MY_VAR='hello'
Dset MY_VAR=hello
Attempts:
2 left
💡 Hint
PowerShell uses a special variable prefix for environment variables.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell script fail on macOS?
This script is intended to list all running processes and filter those using more than 100 MB of memory:
Get-Process | Where-Object { $_.WorkingSet -gt 100MB }

Why does it fail?
PowerShell
Get-Process | Where-Object { $_.WorkingSet -gt 100MB }
A'100MB' is not a valid number; PowerShell does not recognize 'MB' as a unit, causing a syntax error.
BGet-Process is not available on macOS PowerShell.
CThe property 'WorkingSet' does not exist on process objects on macOS.
DWhere-Object cannot filter numeric properties.
Attempts:
2 left
💡 Hint
Check how PowerShell handles numeric values with units.
🚀 Application
advanced
2:30remaining
How to schedule a PowerShell script to run daily on macOS?
You want to run a PowerShell script located at /Users/username/scripts/backup.ps1 every day at 2 AM on macOS. Which approach is correct?
AUse Windows Task Scheduler to schedule the script
BCreate a cron job with: 0 2 * * * pwsh /Users/username/scripts/backup.ps1
CAdd the script to /etc/init.d to run daily
DUse launchd with a plist file to schedule the script
Attempts:
2 left
💡 Hint
macOS uses launchd for scheduled tasks, not cron by default.
🧠 Conceptual
expert
3:00remaining
What is the main difference when running PowerShell scripts on macOS compared to Windows?
Choose the best explanation for a key difference when running PowerShell scripts on macOS versus Windows.
APowerShell on macOS uses a different shell executable and has limited access to Windows-specific cmdlets and APIs.
BPowerShell scripts run identically on macOS and Windows with no differences.
CPowerShell on macOS cannot run scripts at all, only commands interactively.
DPowerShell on macOS requires scripts to be written in Bash syntax.
Attempts:
2 left
💡 Hint
Think about platform-specific features and cmdlets.

Practice

(1/5)
1. Which command do you use to start PowerShell on macOS after installation?
easy
A. pwsh
B. powershell
C. ps
D. shell

Solution

  1. Step 1: Recall the PowerShell command on macOS

    On macOS, PowerShell is started using the pwsh command, not powershell which is used on Windows.
  2. Step 2: Identify the correct command

    Among the options, only pwsh is the correct command to launch PowerShell on macOS.
  3. Final Answer:

    pwsh -> Option A
  4. Quick Check:

    PowerShell on macOS starts with pwsh [OK]
Hint: Remember: macOS uses 'pwsh' to start PowerShell [OK]
Common Mistakes:
  • Typing 'powershell' instead of 'pwsh' on macOS
  • Using 'ps' which lists processes, not PowerShell
  • Assuming 'shell' starts PowerShell
2. Which of the following is the correct syntax to run a PowerShell script named script.ps1 on macOS terminal?
easy
A. ./script.ps1
B. run script.ps1
C. pwsh script.ps1
D. powershell -file script.ps1

Solution

  1. Step 1: Understand how to run scripts in PowerShell on macOS

    On macOS, you run PowerShell scripts by calling pwsh followed by the script name.
  2. Step 2: Check each option

    ./script.ps1 tries to run the script directly, which may fail without execution permission and PowerShell context. powershell -file script.ps1 uses Windows syntax. run script.ps1 is invalid. pwsh script.ps1 correctly runs the script with PowerShell.
  3. Final Answer:

    pwsh script.ps1 -> Option C
  4. Quick Check:

    Run scripts with 'pwsh script.ps1' on macOS [OK]
Hint: Use 'pwsh script.ps1' to run scripts on macOS [OK]
Common Mistakes:
  • Trying to run script directly without 'pwsh'
  • Using Windows PowerShell syntax on macOS
  • Using 'run' command which doesn't exist
3. What will be the output of this PowerShell command run on macOS terminal?
pwsh -Command "Write-Output 'Hello macOS'"
medium
A. Hello macOS
B. Write-Output 'Hello macOS'
C. pwsh: command not found
D. Error: Invalid command

Solution

  1. Step 1: Understand the command structure

    The command uses pwsh -Command to run a PowerShell command inline, which outputs the string 'Hello macOS'.
  2. Step 2: Predict the output

    The Write-Output cmdlet prints the string to the terminal, so the output will be exactly 'Hello macOS'.
  3. Final Answer:

    Hello macOS -> Option A
  4. Quick Check:

    Write-Output prints text to terminal [OK]
Hint: Write-Output prints text; expect exact string output [OK]
Common Mistakes:
  • Confusing command string with output
  • Assuming 'pwsh' is not installed
  • Expecting error due to quotes
4. You try to run a PowerShell script on macOS with pwsh ./myscript.ps1 but get a permission denied error. What is the most likely fix?
medium
A. Use sudo pwsh ./myscript.ps1 to run as admin
B. Run chmod +x myscript.ps1 to add execute permission
C. Rename the script to myscript.sh
D. Reinstall PowerShell on macOS

Solution

  1. Step 1: Identify cause of permission denied

    On macOS, scripts need execute permission to run. Without it, you get a permission denied error.
  2. Step 2: Fix permission issue

    Using chmod +x myscript.ps1 adds execute permission, allowing the script to run.
  3. Final Answer:

    Run chmod +x myscript.ps1 to add execute permission -> Option B
  4. Quick Check:

    Permission denied? Add execute permission with chmod [OK]
Hint: Add execute permission with chmod +x before running script [OK]
Common Mistakes:
  • 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
5. You want to automate listing all files in your Documents folder on macOS using PowerShell and save the output to a text file named files.txt. Which command correctly does this?
hard
A. Get-ChildItem ~/Documents > files.txt
B. ls ~/Documents > files.txt
C. pwsh -Command "ls" ~/Documents > files.txt
D. pwsh -Command "Get-ChildItem ~/Documents | Out-File files.txt"

Solution

  1. Step 1: Use PowerShell cmdlet to list files

    Get-ChildItem lists files and folders in PowerShell. Using ~/Documents targets the Documents folder.
  2. Step 2: Redirect output to a file in PowerShell

    PowerShell uses Out-File to save output to a file. The command runs inside pwsh -Command to execute from macOS terminal.
  3. Final Answer:

    pwsh -Command "Get-ChildItem ~/Documents | Out-File files.txt" -> Option D
  4. Quick Check:

    Use Get-ChildItem with Out-File inside pwsh [OK]
Hint: Use Get-ChildItem piped to Out-File inside pwsh command [OK]
Common Mistakes:
  • 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