Determine Powershell OS, Linux, and Windows Version

How to determine the type of OS (Linux, Windows) using Powershell from a script?

ResponseUri is not recognized when this part of my script runs on a Linux host.

$UrlAuthority = $Request.BaseResponse | Select-Object -ExpandProperty ResponseUri | Select-Object -ExpandProperty Authority 

So, I want the If statement to determine the type of OS that will look something like this:

 If ($OsType -eq "Linux") { $UrlAuthority = ($Request.BaseResponse).RequestMessage | Select-Object -ExpandProperty RequestUri | Select-Object -ExpandProperty host } Else $UrlAuthority = $Request.BaseResponse | Select-Object -ExpandProperty ResponseUri | Select-Object -ExpandProperty Authority 

I could use Get-CimInstance Win32_OperatingSystem , but it would not succeed on Linux since it was not recognized.

+15
source share
6 answers

Are there any environment variables that you can view on other platforms for the OS?

 Get-ChildItem -Path Env: 

In particular, at least Windows has an OS environment variable, so you should be able to do this with $Env:OS


After some time, PowerShell Core is now GA ( 6.0.* ), So you can more accurately define your platform based on the following automatic booleans:

 $IsMacOS $IsLinux $IsWindows 
+24
source

Since PowerShell 6.1 for Windows / Linux / OSX has $PSVersionTable to GA, you can use the new $PSVersionTable , OS , Platform and GitCommitId

Update There are some breaking changes in v6.0.0-beta.3:

  • Change the positional parameter for powershell.exe from -Command to -File

$PSVersionTable on:

Win32NT Platform Microsoft Windows 10.0.15063

 PS C:\Users\LotPings> $PSVersionTable Name Value ---- ----- PSVersion 6.1.0 PSEdition Core GitCommitId 6.1.0 OS Microsoft Windows 10.0.17134 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0 

Linux (ubuntu) Unix Platform Linux (ubuntu)

 PS /home/LotPings> $PSVersionTable Name Value ---- ----- PSVersion 6.1.0 PSEdition Core GitCommitId 6.1.0 OS Linux 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018 Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0 

Darwin Unix Platform

 PS /Users/LotPings> $PSVersionTable Name Value ---- ----- PSVersion 6.1.0 PSEdition Core GitCommitId 6.1.0 OS 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 Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0 
+8
source

In fact, there should be global variables added by the PowerShell console itself - they are not considered environment variables, although, therefore, they will not appear when using dir env: to get a list. which I see now $IsLinux , IsMacOS and $IsWindows . This is at least PowerShell version 6.0.0-rc and above for Mac / Linux.

You can see the list of available with only Get-Variable (in a new session without loading your profile, if you just want what will be set by default).

+5
source

When you only need to check if it is windows or linux, perhaps you could use this (quick and dirty):

 if ([System.Boolean](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue)) { #windows } else { #Not windows } 
+3
source

For PowerShell Core (Powershell Version 6. 0+), you can use Automatic Variables : $IsLinux , $IsMacOS and $IsWindows .

For instance,

 if ($IsLinux) { Write-Host "Linux" } elseif ($IsMacOS) { Write-Host "macOS" } elseif ($IsWindows) { Write-Host "Windows" } 
+1
source

Based on the foregoing, if you only want to determine whether you are running Windows or not, and you need a script that provides forward and backward compatibility in PowerShell and PowerShell Core, follow these steps:

 if ($IsWindows -or $ENV:OS) { Write-Host "Windows" } else { Write-Host "Not Windows" } 
0
source

Source: https://habr.com/ru/post/1269117/


All Articles