Bird
0
0

You wrote this PowerShell script to run only on Windows:

medium📝 Debug Q14 of 15
PowerShell - Cross-Platform PowerShell
You wrote this PowerShell script to run only on Windows:
if ($IsWindows) {
  Write-Output 'Windows detected'
} else {
  Write-Output 'Not Windows'
}
But it always outputs 'Not Windows' even on Windows. What is the likely problem?
AYou ran the script in PowerShell Core on Windows but $IsWindows is true only in Windows PowerShell
BYou ran the script in PowerShell Core on Windows where $IsWindows is false
CYou ran the script in Windows PowerShell 5.1 on Windows where $IsWindows does not exist
DYou used $IsWindows without the $ sign
Step-by-Step Solution
Solution:
  1. Step 1: Understand $IsWindows availability

    The $IsWindows automatic variable exists only in PowerShell Core 6.0+ and is true on Windows.
  2. Step 2: Identify the likely problem

    In legacy Windows PowerShell 5.1, $IsWindows does not exist ($null/false), so the if condition fails even on Windows.
  3. Final Answer:

    You ran the script in Windows PowerShell 5.1 on Windows where $IsWindows does not exist -> Option C
  4. Quick Check:

    $IsWindows unavailable in PS 5.1 [OK]
Quick Trick: Verify your PowerShell edition (powershell.exe vs pwsh.exe) [OK]
Common Mistakes:
  • Forgetting $ sign on variable
  • Assuming $IsWindows always true on Windows
  • Confusing PowerShell Core and Windows PowerShell behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes