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
Platform-specific considerations
📖 Scenario: You are writing a PowerShell script that needs to behave differently depending on the operating system it runs on. This is common when scripts must work on Windows, Linux, or macOS.
🎯 Goal: Build a PowerShell script that detects the platform it is running on and stores the platform name in a variable. Then, use this information to create a message that tells the user which platform the script detected.
📋 What You'll Learn
Create a variable called platform that stores the current operating system name.
Create a variable called message that uses platform to build a message string.
Use a switch statement to set platform based on the OS.
Print the message to the console.
💡 Why This Matters
🌍 Real World
Scripts often need to behave differently on Windows, Linux, or macOS. Detecting the platform helps automate tasks correctly.
💼 Career
Knowing how to write cross-platform scripts is valuable for system administrators and automation engineers who manage diverse environments.
Progress0 / 4 steps
1
Detect the operating system
Create a variable called osName that stores the value of [System.Environment]::OSVersion.Platform.
PowerShell
Hint
Use [System.Environment]::OSVersion.Platform to get the platform identifier.
2
Set the platform variable
Create a variable called platform and initialize it with an empty string ''.
PowerShell
Hint
Initialize platform as an empty string before setting it based on osName.
3
Use switch to assign platform name
Use a switch statement on $osName with cases 2 for 'Windows', 4 for 'Unix', 6 for 'MacOSX', and default for 'Windows'. Assign the matching string to $platform inside each case.
PowerShell
Hint
The platform codes 2 mean Windows, 4 mean Unix (Linux), 6 means MacOSX, and anything else is Windows.
4
Print the platform message
Create a variable called message that uses a double-quoted string to say "The script is running on: $platform". Then print $message.
PowerShell
Hint
Use double quotes to allow variable expansion in the string. Use Write-Output to print.
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]