3

I use uname -s in bash scripts to determine the OS and it returns Linux, Darwin or MINGW64_NT... when its running on Linux, macOS or Windows.

EDIT0 : I want my $PROFILE script to detect the OS whether is running on Windows with PS (version could be lower than 6) or Linux with PSv>=6.

I found this in powershell :

PS> [System.Environment]::OSVersion.Platform

On Linux, it returns Unix and on a 64bits Windows, it returns Win32NT.

I don't have a macOS at my disposal (not yet:)) so I don't know what it actually returns on macOS.

EDIT1 : This method doesn't seem to different between Unix and Linux or Windows32b and Windows64b.

What other ways are there to detect the OS in powershell 5.1 ?

SebMa
  • 4,037
  • 29
  • 39
  • 5.1 doesn’t run on macOS or Linux. There are other ways to get OS info, is the approach shown not sufficient? – Doug Maurer Nov 18 '20 at 17:21
  • @DougMaurer You're right. On my Linux, I have PSv7, but on my win7, I have PSv5.1. But I want my `$PROFILE` script to detect the OS wheter it's running on PS5 or PS7. – SebMa Nov 18 '20 at 19:56

1 Answers1

6

The problem is Powershell 5.1.

Detailed Here: https://devblogs.microsoft.com/scripting/powertip-determine-your-version-of-powershell-and-host-operating-system/

More Detail Here: Determine the OS version, Linux and Windows from Powershell

Powershell 5.1 does not have the capacity nor ability to determine OS outside of the Microsoft environment. The best you can do is, using get-wmi or cim-sesssion

 windows 
 !windows

For PowerShell Core (Powershell Version 6.0+), you can use Automatic Variables:

$IsLinux
$IsMacOS
$IsWindows

With 6+ you can do something to the effect of:

   foreach ($i in $info) {
    
    if ($i -eq $IsLinux) {
        Write-Host $i is Linux
    }
    elseif ($i -eq $IsMacOS) {
        Write-Host $i is This is a dirty, dirty Mac
    }
    elseif ($i -eq $IsWindows) {
        Write-Host $i is Windows
    }

   }

To bring this to a close what you are asking for is simply not possible / possibly not worth the effort with PowerShell 5.1.

iNet
  • 126
  • 5
  • I see. What is this `$info` variable that you're looping through containing ? – SebMa Nov 18 '20 at 20:56
  • I don't understant the `get-wmi` and `cim-sesssion` commands. There are not recognized by PowerShell nor by `cmd.exe` – SebMa Nov 18 '20 at 21:24
  • $info variable would be device names or IP address array or similar from a get-content – iNet Nov 19 '20 at 12:22
  • Get-WmiObject you can connect to a Windows computer and get information. In PowerShell 5.1's case you can determine "All Windows" computers, and request information from them, and with that, determine which ones are not windows. However, you cannot determine beyond that as Linux/macOS/etc does not support Get-WmiObject. The issue is of late not using Get-WmiObject and to use Get-CimInstance instead. [Great examples of using Get-CimInstance](https://learn.microsoft.com/en-us/powershell/scripting/samples/collecting-information-about-computers?view=powershell-7.1) – iNet Nov 19 '20 at 12:43
  • I don't think the code block makes any sense. `$IsLinux`/`$IsWindows` are variables set to either `True` or `False` by the current shell. They are not properties of WMI classes! – xjcl Nov 28 '22 at 17:30