0
0
PowerShellscripting~15 mins

Platform-specific considerations in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use double quotes to allow variable expansion in the string. Use Write-Output to print.