A PowerShell script I am working on requires different behavior across operating system platforms.

Which is to say:

  • Windows
  • Unix

For this, we turn to the static property OSVersion of the Environment object, where we retrieve the Platform property

The script will look like this:

$platform = [System.Environment]::OSVersion.Platform 

This will return the following across 3 operating systems.

Operating System Value
Windows 11 Win32NT
macOS 15 (Sequoia) Unix
Ubuntu 24.04 Unix

Of interest is that Ubuntu, a Linux distribution, reports Unix.

We’ll look into why that is the case in the next post.

TLDR

PowerShell can use the Environment object to retrieve the OS Platform.

Happy hacking!