0
0
PowerShellscripting~20 mins

Platform-specific considerations in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.