Determine the OS version, Linux and Windows from Powershell
Aren't there environment variables you can view on the other platforms for the OS?
Get-ChildItem -Path Env:
Particularly, on Windows at least, there's an OS environment variable, so you should be able to accomplish this by using $Env:OS
.
Since some time has passed and the PowerShell Core (v6) product is GA now (the Core branding has been dropped as of v7), you can more accurately determine your platform based on the following automatic boolean variables:
$IsMacOS$IsLinux$IsWindows
Since the PowerShell versions 6.1 on Windows/Linux/OSX went to GA you can use the new properties of $PSVersionTable
, OS
, Platform
and GitCommitId
Update In v6.0.0-beta.3 there are some breaking changes
:
- Change positional parameter for powershell.exe from -Command to -File
$PSVersionTable
on :
Platform Win32NT
OS Microsoft Windows 10.0.15063
PS C:\Users\LotPings> $PSVersionTableName Value---- -----PSVersion 6.1.0PSEdition CoreGitCommitId 6.1.0OS Microsoft Windows 10.0.17134Platform Win32NTPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}PSRemotingProtocolVersion 2.3SerializationVersion 1.1.0.1WSManStackVersion 3.0
Platform Unix
OS Linux (ubuntu)
PS /home/LotPings> $PSVersionTableName Value---- -----PSVersion 6.1.0PSEdition CoreGitCommitId 6.1.0OS Linux 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018Platform UnixPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}PSRemotingProtocolVersion 2.3SerializationVersion 1.1.0.1WSManStackVersion 3.0
Platform Unix
OS Darwin
PS /Users/LotPings> $PSVersionTableName Value---- -----PSVersion 6.1.0PSEdition CoreGitCommitId 6.1.0OS Darwin 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RE...Platform UnixPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}PSRemotingProtocolVersion 2.3SerializationVersion 1.1.0.1WSManStackVersion 3.0
For PowerShell Core (Powershell Version 6.0+), you can use Automatic Variables: $IsLinux
, $IsMacOS
and $IsWindows
.
For example,
if ($IsLinux) { Write-Host "Linux"}elseif ($IsMacOS) { Write-Host "macOS"}elseif ($IsWindows) { Write-Host "Windows"}