Bird
Raised Fist0
PowerShellscripting~20 mins

Platform-specific considerations in PowerShell - 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 Platform Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
PowerShell script output on Windows vs Linux
What is the output of this PowerShell script when run on Windows and Linux respectively?
PowerShell
Write-Output $PSVersionTable.PSPlatform
A"WindowsNT" on Windows, "Unix" on Linux
B"Windows" on Windows, "Linux" on Linux
C"Win32" on Windows, "Linux" on Linux
D"Win32NT" on Windows, "Unix" on Linux
Attempts:
2 left
💡 Hint
Check the $PSVersionTable automatic variable for platform info.
📝 Syntax
intermediate
2:00remaining
Correct path separator in PowerShell scripts
Which option correctly sets a file path variable that works on both Windows and Linux in PowerShell?
A$path = "C:\Users\Public\Documents\file.txt"
B$path = "C:/Users/Public/Documents/file.txt"
C$path = Join-Path -Path 'C:' -ChildPath 'Users/Public/Documents/file.txt'
D$path = Join-Path -Path 'C:' -ChildPath 'Users\Public\Documents\file.txt'
Attempts:
2 left
💡 Hint
PowerShell accepts forward slashes as path separators on all platforms.
🔧 Debug
advanced
2:00remaining
Identify the error in this cross-platform PowerShell script
What error will this script produce when run on Linux PowerShell?
PowerShell
if (Test-Path "C:\Windows") { Write-Output "Windows folder exists" } else { Write-Output "Folder not found" }
AOutputs "Folder not found"
BOutputs "Windows folder exists"
CThrows a runtime error because the path is invalid on Linux
DThrows a syntax error due to backslashes
Attempts:
2 left
💡 Hint
Consider how Test-Path behaves with Windows paths on Linux.
🚀 Application
advanced
2:00remaining
Write a PowerShell snippet to detect OS and print a message
Which snippet correctly detects the OS platform and prints 'Running on Windows' or 'Running on Linux' accordingly?
Aif ($IsWindows) { Write-Output 'Running on Windows' } else { Write-Output 'Running on Linux' }
Bif ($env:OS -eq 'Windows_NT') { Write-Output 'Running on Windows' } else { Write-Output 'Running on Linux' }
Cswitch ($PSVersionTable.PSPlatform) { 'Win32NT' { Write-Output 'Running on Windows' }; 'Unix' { Write-Output 'Running on Linux' } }
Dif ($PSVersionTable.Platform -eq 'Windows') { Write-Output 'Running on Windows' } else { Write-Output 'Running on Linux' }
Attempts:
2 left
💡 Hint
Use $PSVersionTable.PSPlatform for reliable platform detection.
🧠 Conceptual
expert
2:00remaining
Why is using environment variables for platform detection preferred in PowerShell scripts?
Which reason best explains why environment variables like $env:OS are less reliable than $PSVersionTable.PSPlatform for platform detection in PowerShell?
A$env:OS is not set on Linux and macOS, causing inconsistent results.
B$PSVersionTable.PSPlatform is deprecated and should not be used.
C$env:OS always returns 'Windows_NT' regardless of platform.
D$PSVersionTable.PSPlatform only works on Windows PowerShell, not PowerShell Core.
Attempts:
2 left
💡 Hint
Think about environment variables availability across platforms.

Practice

(1/5)
1. Which automatic variable in PowerShell helps you detect if the script is running on Windows?
easy
A. $IsLinux
B. $IsWindows
C. $IsMacOS
D. $Platform

Solution

  1. Step 1: Understand platform detection variables

    PowerShell provides automatic variables like $IsWindows, $IsLinux, and $IsMacOS to detect the current OS.
  2. Step 2: Identify the variable for Windows

    $IsWindows is true only when running on Windows, while others are for Linux or MacOS.
  3. Final Answer:

    $IsWindows -> Option B
  4. Quick Check:

    Windows detection = $IsWindows [OK]
Hint: Remember $IsWindows is true only on Windows systems [OK]
Common Mistakes:
  • Confusing $IsLinux or $IsMacOS as Windows variables
  • Using $Platform which does not exist
  • Assuming $IsWindows works on Linux or Mac
2. Which of the following is the correct syntax to run platform-specific code only on Linux in PowerShell?
easy
A. if ($IsUnix) { Write-Host 'Running on Linux' }
B. if ($IsWindows) { Write-Host 'Running on Linux' }
C. if ($IsMacOS) { Write-Host 'Running on Linux' }
D. if ($IsLinux) { Write-Host 'Running on Linux' }

Solution

  1. Step 1: Identify the variable for Linux

    $IsLinux is the automatic variable that is true only on Linux systems.
  2. Step 2: Check the syntax for conditional execution

    The syntax if ($IsLinux) { ... } runs the block only on Linux.
  3. Final Answer:

    if ($IsLinux) { Write-Host 'Running on Linux' } -> Option D
  4. Quick Check:

    Linux code block = if ($IsLinux) [OK]
Hint: Use if ($IsLinux) for Linux-specific code blocks [OK]
Common Mistakes:
  • Using $IsWindows or $IsMacOS for Linux code
  • Using undefined variable $IsUnix
  • Incorrect if statement syntax
3. What will be the output of this PowerShell script when run on macOS?
if ($IsWindows) { Write-Output 'Windows' } elseif ($IsLinux) { Write-Output 'Linux' } elseif ($IsMacOS) { Write-Output 'MacOS' } else { Write-Output 'Unknown' }
medium
A. MacOS
B. Linux
C. Windows
D. Unknown

Solution

  1. Step 1: Understand the platform variables

    On macOS, $IsMacOS is true, while $IsWindows and $IsLinux are false.
  2. Step 2: Follow the conditional logic

    The script checks $IsWindows (false), then $IsLinux (false), then $IsMacOS (true), so it outputs 'MacOS'.
  3. Final Answer:

    MacOS -> Option A
  4. Quick Check:

    macOS detection outputs 'MacOS' [OK]
Hint: Check $IsMacOS true for Mac output in if-elseif chain [OK]
Common Mistakes:
  • Assuming Linux output on macOS
  • Ignoring else block
  • Confusing $IsWindows with $IsMacOS
4. 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?
medium
A. You ran the script in PowerShell Core on Windows but $IsWindows is true only in Windows PowerShell
B. You ran the script in PowerShell Core on Windows where $IsWindows is false
C. You ran the script in Windows PowerShell 5.1 on Windows where $IsWindows does not exist
D. You used $IsWindows without the $ sign

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]
Hint: 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
5. You want to write a PowerShell script that creates a folder named 'Logs' only on Windows and macOS, but skips Linux. Which code snippet correctly implements this platform-specific behavior?
hard
A. if ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' }
B. if ($IsLinux) { New-Item -ItemType Directory -Path './Logs' }
C. if ($IsWindows -and $IsMacOS) { New-Item -ItemType Directory -Path './Logs' }
D. if ($IsUnix) { New-Item -ItemType Directory -Path './Logs' }

Solution

  1. Step 1: Understand platform conditions

    You want to create the folder only on Windows or macOS, so the condition should check if either $IsWindows or $IsMacOS is true.
  2. Step 2: Analyze each option

    if ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } uses -or to combine $IsWindows and $IsMacOS correctly. if ($IsLinux) { New-Item -ItemType Directory -Path './Logs' } creates folder only on Linux (wrong). if ($IsWindows -and $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } uses -and which requires both true (impossible). if ($IsUnix) { New-Item -ItemType Directory -Path './Logs' } uses undefined $IsUnix which is falsey (wrong).
  3. Final Answer:

    if ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } -> Option A
  4. Quick Check:

    Use -or for Windows or Mac condition [OK]
Hint: Use -or to combine platform checks for multiple OS [OK]
Common Mistakes:
  • Using -and instead of -or for multiple platforms
  • Creating folder on Linux by mistake
  • Using undefined variables like $IsUnix