Bird
Raised Fist0
PowerShellscripting~20 mins

PowerShell on Linux - 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 Linux Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
PowerShell command output on Linux
What is the output of this PowerShell command run on a typical Linux system?
Get-Process | Where-Object { $_.CPU -gt 0 } | Select-Object -First 1 -Property Name,CPU
PowerShell
Get-Process | Where-Object { $_.CPU -gt 0 } | Select-Object -First 1 -Property Name,CPU
A@{Name=systemd; CPU=0.03125}
BNo output, command fails on Linux
C@{Name=powershell; CPU=0.0}
D@{Name=code; CPU=0.015625}
Attempts:
2 left
💡 Hint
Think about common Linux processes that consume CPU and appear in Get-Process.
📝 Syntax
intermediate
1:30remaining
Correct syntax for reading a file in PowerShell on Linux
Which option correctly reads the contents of a text file named 'log.txt' in the current directory on Linux using PowerShell?
ARead-Content log.txt
Bcat log.txt | Read-File
CGet-Content ./log.txt
DGet-FileContent log.txt
Attempts:
2 left
💡 Hint
PowerShell uses a cmdlet to read file contents, similar to cat but with a different name.
🔧 Debug
advanced
2:00remaining
Fix the error in this PowerShell script on Linux
This script is intended to list all files in /tmp and output their names, but it throws an error on Linux. What is the cause?
Get-ChildItem -Path C:\tmp | ForEach-Object { $_.Name }
PowerShell
Get-ChildItem -Path C:\tmp | ForEach-Object { $_.Name }
AGet-ChildItem cannot list files on Linux.
BThe path uses Windows-style backslashes; use /tmp instead.
CForEach-Object is not supported on Linux PowerShell.
DThe script is missing a pipeline to Out-File.
Attempts:
2 left
💡 Hint
Linux uses forward slashes in paths, unlike Windows.
🚀 Application
advanced
2:30remaining
Automate service restart with PowerShell on Linux
You want to restart the 'nginx' service on a Linux system using PowerShell. Which command correctly does this?
ARestart-Process nginx
BInvoke-Expression 'sudo systemctl restart nginx'
Csystemctl restart nginx
DRestart-Service nginx
Attempts:
2 left
💡 Hint
PowerShell provides cross-platform cmdlets for service management on Linux.
🧠 Conceptual
expert
2:00remaining
Understanding PowerShell environment differences on Linux
Which statement best describes a key difference when running PowerShell scripts on Linux compared to Windows?
APowerShell on Linux uses case-sensitive file paths and supports native Linux commands.
BPowerShell on Linux cannot run native Linux commands directly.
CPowerShell on Linux uses Windows registry to store configuration settings.
DPowerShell on Linux does not support modules or scripts.
Attempts:
2 left
💡 Hint
Think about how Linux file systems differ from Windows and how PowerShell integrates with the OS.

Practice

(1/5)
1. What command do you use to start PowerShell on a Linux system?
easy
A. pwsh
B. powershell.exe
C. start-ps
D. shellps

Solution

  1. Step 1: Recall the PowerShell start command on Linux

    On Linux, PowerShell is started by typing pwsh in the terminal.
  2. Step 2: Compare options with the known command

    powershell.exe, start-ps, and shellps are not valid commands to start PowerShell on Linux.
  3. Final Answer:

    pwsh -> Option A
  4. Quick Check:

    PowerShell start command on Linux = pwsh [OK]
Hint: Remember: 'pwsh' starts PowerShell on Linux terminals [OK]
Common Mistakes:
  • Typing 'powershell.exe' which is for Windows only
  • Using 'start-ps' which is not a valid command
  • Confusing shell names
2. Which of the following is the correct syntax to run a Linux command inside PowerShell on Linux?
easy
A. Run-Command 'ls -l'
B. Invoke-Linux ls -l
C. ls -l
D. Start-Linux ls -l

Solution

  1. Step 1: Understand how Linux commands run in PowerShell on Linux

    PowerShell on Linux allows running Linux commands directly by typing them as is, like ls -l.
  2. Step 2: Evaluate each option

    Invoke-Linux ls -l, Run-Command 'ls -l', and Start-Linux ls -l are not valid syntax to run Linux commands.
  3. Final Answer:

    ls -l -> Option C
  4. Quick Check:

    Run Linux commands directly in PowerShell = ls -l [OK]
Hint: Run Linux commands directly without extra syntax in PowerShell [OK]
Common Mistakes:
  • Adding unnecessary PowerShell cmdlets before Linux commands
  • Using quotes incorrectly around Linux commands
  • Assuming Linux commands need special wrappers
3. What will be the output of this PowerShell on Linux command sequence?
pwsh
$files = ls /etc | Where-Object { $_.Name -like '*.conf' }
$files.Count
medium
A. Always zero because filtering is incorrect
B. An error because ls is not recognized
C. The list of all files in /etc
D. The number of files in /etc ending with .conf

Solution

  1. Step 1: Understand the command sequence

    The command ls /etc lists files in /etc. The Where-Object filters files whose names end with '.conf'.
  2. Step 2: Determine the output of $files.Count

    $files stores the filtered list, so $files.Count returns the number of such files.
  3. Final Answer:

    The number of files in /etc ending with .conf -> Option D
  4. Quick Check:

    Count filtered files = number [OK]
Hint: Count filtered files with .Count property after Where-Object [OK]
Common Mistakes:
  • Thinking ls is not recognized in PowerShell on Linux
  • Assuming filtering syntax is invalid
  • Confusing output with file list instead of count
4. You run this script in PowerShell on Linux:
pwsh
$process = Get-Process -Name "bash"
Write-Output $process.Id
But it returns an error: "Get-Process: The term 'Get-Process' is not recognized." What is the likely cause?
medium
A. PowerShell is not installed correctly on Linux
B. You are running the script in the Linux shell, not inside PowerShell
C. The process 'bash' does not exist
D. Get-Process cmdlet is not available on Linux PowerShell

Solution

  1. Step 1: Analyze the error message

    The error says 'Get-Process' is not recognized, which usually means the command is run outside PowerShell.
  2. Step 2: Understand environment context

    If you run PowerShell commands in the Linux shell (bash), they won't work. You must be inside PowerShell (started with pwsh) to run Get-Process.
  3. Final Answer:

    You are running the script in the Linux shell, not inside PowerShell -> Option B
  4. Quick Check:

    Run PowerShell cmdlets inside pwsh shell [OK]
Hint: Run PowerShell commands only inside pwsh shell, not bash [OK]
Common Mistakes:
  • Assuming Get-Process is missing on Linux PowerShell
  • Thinking the process 'bash' does not exist
  • Not starting PowerShell before running commands
5. You want to write a PowerShell script on Linux that lists all running processes and filters only those owned by the current user. Which approach is correct?
hard
A. Get-Process | Where-Object { $_.UserName -eq $env:USER }
B. ps -u $USER | pwsh
C. Get-Process | Where-Object { $_.UserName -eq (whoami) }
D. Get-Process | Where-Object { $_.UserName -eq $env:USERNAME }

Solution

  1. Step 1: Identify environment variable for current user in PowerShell on Linux

    PowerShell on Linux uses $env:USER to get the current user's name.
  2. Step 2: Check filtering syntax for processes

    Filtering processes by Where-Object { $_.UserName -eq $env:USER } correctly compares the process owner to the current user.
  3. Step 3: Evaluate other options

    ps -u $USER | pwsh mixes Linux command with PowerShell incorrectly. Get-Process | Where-Object { $_.UserName -eq (whoami) } uses command output that may not match precisely. Get-Process | Where-Object { $_.UserName -eq $env:USERNAME } uses $env:USERNAME which is typically not set on Linux.
  4. Final Answer:

    Get-Process | Where-Object { $_.UserName -eq $env:USER } -> Option A
  5. Quick Check:

    Use $env:USER for current user in PowerShell on Linux [OK]
Hint: Use $env:USER to get current user in PowerShell on Linux [OK]
Common Mistakes:
  • Using $env:USERNAME which isn't set on Linux PowerShell
  • Mixing Linux commands like 'ps -u $USER' with PowerShell incorrectly
  • Using command output like (whoami) without precise matching