Introduction
Different computers and systems work in different ways. Knowing platform-specific details helps your script run correctly everywhere.
Jump into concepts and practice - no test required
if ($IsWindows) { # Windows-specific code } elseif ($IsLinux) { # Linux-specific code } elseif ($IsMacOS) { # macOS-specific code }
if ($IsWindows) { Write-Output "Running on Windows" } else { Write-Output "Not Windows" }
switch ($PSVersionTable.Platform) {
'Win32NT' { Write-Output "Windows platform" }
'Unix' { Write-Output "Unix-like platform" }
default { Write-Output "Unknown platform" }
}if ($IsWindows) { Write-Output "This script runs on Windows." } elseif ($IsLinux) { Write-Output "This script runs on Linux." } elseif ($IsMacOS) { Write-Output "This script runs on macOS." } else { Write-Output "Unknown platform." }
if ($IsWindows) { Write-Output 'Windows' } elseif ($IsLinux) { Write-Output 'Linux' } elseif ($IsMacOS) { Write-Output 'MacOS' } else { Write-Output 'Unknown' }if ($IsWindows) {
Write-Output 'Windows detected'
} else {
Write-Output 'Not Windows'
}
But it always outputs 'Not Windows' even on Windows. What is the likely problem?