How to find the OS on Mac, Windows and Linux

Welcome Forums General PowerShell Q&A How to find the OS on Mac, Windows and Linux

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #61920

    Participant
    Points: 1
    Rank: Member

    Now that PowerShell is cross-platform is there a way to get the OS and Version? I would like to send that information in the User-Agent parameter of my Invoke-RestMethod.

  • #61921

    Keymaster
    Points: 1,673
    Helping HandTeam Member
    Rank: Community Hero

    So, it's probably worth knowing that PowerShell CORE is what's cross-platform, not "all" PowerShell. And PowerShell is separate from, say, CIM/WMI/OMI. It's also worth pointing out that PowerShell is intended to act as a kind of a wrapper around whatever the OS does natively – not necessarily replace what the OS does natively.

    So let's so you're on Linux. You can't use Get-WmiObject, obviously. However, if your Linux has OMI installed, you could maybe use Get-CimInstance. If your distro implements the CIM_OperatingSystem class, for example, that would tell you.

    Also have a look at https://www.cyberciti.biz/faq/find-linux-distribution-name-version-number/. There are several commands, which you can obviously run inside PowerShell, that would get you the OS info. Linux tends to keep everything in text files, so as long as you know which text file to look in, retrieving information isn't usually too hard.

    • #61924

      Participant
      Points: 1
      Rank: Member

      Here are a few options I got from twitter.

      Add-Type -AssemblyName Systm.Runtime.InteropServices.RuntimeInformation
      [Systm.Runtime.InteropServices.RuntimeInformation]::OSDescription
      
      $os = 'unknown'
      
      if ($PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows) {
         $os = 'Windows'
      } elseif ($IsOSX) {
         $os = 'OSX'
      } elseif ($IsLinux) {
         $os = 'Linux'
      }
      
      & bash -c `uname`;
      

The topic ‘How to find the OS on Mac, Windows and Linux’ is closed to new replies.