Bird
0
0

This script is intended to run on Windows and Linux but uses a Windows-only environment variable. What is the problem?

medium📝 Debug Q14 of 15
PowerShell - Cross-Platform PowerShell
This script is intended to run on Windows and Linux but uses a Windows-only environment variable. What is the problem?
if ($env:OS -eq 'Windows_NT') { Write-Host 'Windows' } else { Write-Host 'Linux or macOS' }
AThe else block syntax is incorrect
BMissing parentheses around condition
CWrite-Host is not supported on Linux
DUsing $env:OS is Windows-only and undefined on Linux
Step-by-Step Solution
Solution:
  1. Step 1: Check environment variable usage

    $env:OS is defined only on Windows, so on Linux it is empty or undefined.
  2. Step 2: Understand the problem with $env:OS

    Because $env:OS is missing on Linux, the script relies on a Windows-specific variable, making it non-portable.
  3. Final Answer:

    Using $env:OS is Windows-only and undefined on Linux -> Option D
  4. Quick Check:

    $env:OS is Windows-only env var [OK]
Quick Trick: Avoid Windows-only env vars for cross-platform scripts [OK]
Common Mistakes:
  • Thinking Write-Host is unsupported on Linux
  • Believing parentheses are required around if condition
  • Assuming else syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes