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
Recall & Review
beginner
What is a key platform-specific consideration when writing PowerShell scripts?
Different operating systems have different file paths, commands, and environment variables. PowerShell scripts must handle these differences to work correctly across platforms.
Click to reveal answer
beginner
How does PowerShell handle file paths differently on Windows vs Linux?
Windows uses backslashes (\) in file paths, while Linux uses forward slashes (/). PowerShell provides automatic path conversion or you can use the Join-Path cmdlet to handle paths correctly.
Click to reveal answer
intermediate
Why should you check the platform inside a PowerShell script?
To run platform-specific commands or adjust behavior, you can check the platform using the automatic variable $PSVersionTable.PSEdition or [System.Environment]::OSVersion.Platform.
Click to reveal answer
intermediate
What is a common difference in environment variables between Windows and Linux in PowerShell scripts?
Windows environment variables are case-insensitive and accessed like $env:Path, while Linux variables are case-sensitive. Also, some variables exist only on one platform.
Click to reveal answer
beginner
How can you write a PowerShell script that works on both Windows and Linux?
Use platform checks to run platform-specific code, use cmdlets like Join-Path for paths, avoid Windows-only commands, and test scripts on both platforms.
Click to reveal answer
Which cmdlet helps handle file paths correctly across platforms in PowerShell?
ASet-Location
BGet-ChildItem
CNew-Item
DJoin-Path
✗ Incorrect
Join-Path combines parts of a file path using the correct separator for the platform.
What symbol does Linux use for file paths that differs from Windows?
A/
B\
C:
D|
✗ Incorrect
Linux uses forward slashes (/) in file paths, unlike Windows which uses backslashes (\).
How can you detect the operating system platform inside a PowerShell script?
A$PSVersionTable.PSEdition
B$env:OS
CBoth A and D
D[System.Environment]::OSVersion.Platform
✗ Incorrect
Both $PSVersionTable.PSEdition and [System.Environment]::OSVersion.Platform can be used to detect the platform.
Which environment variable behavior differs between Windows and Linux in PowerShell?
AVariable name prefix
BCase sensitivity
CVariable type
DVariable length
✗ Incorrect
Windows environment variables are case-insensitive, while Linux environment variables are case-sensitive.
What is a good practice to make PowerShell scripts cross-platform?
AUse platform detection and platform-agnostic cmdlets
BAvoid platform checks
CUse Windows-only commands
DHardcode file paths
✗ Incorrect
Using platform detection and platform-agnostic cmdlets like Join-Path helps scripts run on multiple platforms.
Explain why platform-specific considerations are important when writing PowerShell scripts.
Think about how Windows and Linux handle files and commands differently.
You got /4 concepts.
Describe how you would write a PowerShell script that works on both Windows and Linux.
Consider how to handle differences in commands and file paths.
You got /4 concepts.
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
Step 1: Understand platform detection variables
PowerShell provides automatic variables like $IsWindows, $IsLinux, and $IsMacOS to detect the current OS.
Step 2: Identify the variable for Windows
$IsWindows is true only when running on Windows, while others are for Linux or MacOS.
Final Answer:
$IsWindows -> Option B
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
Step 1: Identify the variable for Linux
$IsLinux is the automatic variable that is true only on Linux systems.
Step 2: Check the syntax for conditional execution
The syntax if ($IsLinux) { ... } runs the block only on Linux.
Final Answer:
if ($IsLinux) { Write-Host 'Running on Linux' } -> Option D
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?
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
Step 1: Understand $IsWindows availability
The $IsWindows automatic variable exists only in PowerShell Core 6.0+ and is true on Windows.
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.
Final Answer:
You ran the script in Windows PowerShell 5.1 on Windows where $IsWindows does not exist -> Option C
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
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.
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).
Final Answer:
if ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } -> Option A
Quick Check:
Use -or for Windows or Mac condition [OK]
Hint: Use -or to combine platform checks for multiple OS [OK]