Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print a message about cross-platform reach.
PowerShell
Write-Output [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the string in quotes.
Using echo instead of Write-Output in PowerShell.
✗ Incorrect
In PowerShell, strings must be in quotes to be printed correctly with Write-Output.
2fill in blank
mediumComplete the code to check the operating system platform.
PowerShell
$platform = [System.Environment]::OSVersion.Platform; if ($platform -eq [1]) { Write-Output "Windows detected" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong string for Windows platform.
Confusing platform strings with other OSes.
✗ Incorrect
In PowerShell Core, the platform value "Win32NT" represents Windows OS.
3fill in blank
hardFix the error in the script to detect Linux platform.
PowerShell
if ([System.Environment]::OSVersion.Platform -eq [1]) { Write-Output "Linux detected" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "Win32NT" or other strings which represent other platforms.
Not using the correct platform string for Linux.
✗ Incorrect
Platform value "Unix" represents Unix/Linux systems in PowerShell Core.
4fill in blank
hardFill both blanks to create a cross-platform script that prints OS name.
PowerShell
$os = (Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue).[1]; if (-not $os) { $os = (uname).[2] }; Write-Output $os
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names for OS detection.
Not converting uname output to string.
✗ Incorrect
On Windows, 'Caption' gives the OS name; on Unix, 'uname' output converted to string shows OS.
5fill in blank
hardFill all three blanks to create a script that runs a command based on OS platform.
PowerShell
switch ([System.Environment]::OSVersion.Platform) { "Win32NT" { [1] 'dir' } "Unix" { [2] 'ls' } default { [3] 'Unsupported OS' } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Write-Output instead of running commands.
Not handling default case properly.
✗ Incorrect
Invoke-Expression runs 'dir' (PowerShell alias for Get-ChildItem) on Windows; Invoke-Expression runs 'ls' on Unix/Linux; Write-Output prints message for unsupported OS.