Bird
0
0

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📝 Application Q15 of 15
PowerShell - Cross-Platform PowerShell
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?
AGet-Process | Where-Object { $_.UserName -eq $env:USER }
Bps -u $USER | pwsh
CGet-Process | Where-Object { $_.UserName -eq (whoami) }
DGet-Process | Where-Object { $_.UserName -eq $env:USERNAME }
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes