PowerShell Déterminer le système d'exploitation

What is the system: Windows? Linux? macOS?

I. Presentation

In this tutorial, we will discover three automatic variables available with PowerShell: $IsWindows, $IsLinux and $IsMacOS. These variables are available from PowerShell 7.0, which is not trivial, since Microsoft has been offering cross-platform versions of PowerShell for several years. Indeed, since PowerShell Core 6.0 was released, PowerShell runs on Windows, Linux, and macOS thanks to the fact that it is based on .NET Core, unlike Windows PowerShell which is based on .NET Framework.

These variables are very handy, because they let you know if the script is running on a Windows, Linux, or macOS machine. This makes it easier to write PowerShell scripts compatible with several totally different systems.

Remark : these variables are not available with Windows PowerShell 5.1 (and earlier versions)

II. Discovery of $IsWindows, $IsLinux and $IsMacOS

These three automatic variables are managed by PowerShell, like other variables of this type ($Error, $_, etc.). These variables return a boolean, namely “true” (true) or “false” (false) depending on the operating system of the machine.

To be more precise, it gives:

Is equal to “true” if the current PowerShell session is on a machine Windowsotherwise it is equal to “false”.

Is equal to “true” if the current PowerShell session is on a machine Linuxotherwise it is equal to “false”.

Is equal to “true” if the current PowerShell session is on a machine macOSotherwise it is equal to “false”.

Do we do a test? This is what it looks like on my Windows machine:

PowerShell - Variables isWindows isLinux isMacOS

III. Run different code depending on the system

Therefore, we can imagine a script in which we will execute a different code depending on the system. If we take the example of a file to be downloaded and copied, the paths will not be the same between the different systems, hence the interest of using this type of variable.

if($isWindows -eq $true){

   Write-Output "Système Windows - Le traitement va continuer."
   # Instructions

}elseif($isLinux -eq $true){

   Write-Output "Système Linux - Le traitement va continuer."
   # Instructions

}elseif($isMacOS -eq $true){

   Write-Output "Système macOS - Le traitement va continuer."
   # Instructions

   Write-Warning "Système d'exploitation inconnu"

Using this example, you can add the instructions that are fine, depending on the system.

We could also check the system in order to run the script if it is indeed a Windows machine and D’stop processing if it is another system. Of course, we can do the same thing by checking another system, changing automatic variable.

Which give :

   Write-Output "Système sous Windows, le script va se poursuivre..."
   # Instructions

   throw "Arrêt du traitement : OS non Windows"

The fact of using “throw” generates a fatal error if it is not Windows. As a result, the script stops.

IV. Conclusion

This PowerShell tip can come in handy in some situations, especially since it’s easy to implement and use. Now you can no longer ignore variables $IsWindows, $IsLinux et $IsMacOS.


#system #Windows #Linux #macOS

Leave a Comment

Your email address will not be published. Required fields are marked *