Posey's Tips & Tricks

How To Create Cross-Platform PowerShell Scripts

To avoid errors, it's important to write PowerShell scripts that prevent code from running on an unintended platform. Luckily, this is easier to do than it sounds.

Up until a few years ago, PowerShell scripts could only run on Windows machines. Admittedly, that sounds like a really obvious and inconsequential statement, but think about what it means: Back then, it was possible to create a PowerShell script without thinking about what kind of machine the script would eventually run on.

Today, this simply is not the case. A PowerShell script that is intended for a Windows machine probably isn't going to work correctly on a Linux machine. Of course, the opposite is also true.

Actually, it's a little bit worse than that. If you attempt to run a PowerShell script on a different platform than it was intended for, some of the commands within the script will probably work, while others are unsupported and will therefore generate errors. The end result will be completely unpredictable. In some cases, the script could end up doing damage.

That being the case, it is important to build PowerShell scripts that are either cross-platform, or that prevent code from running on an unintended platform. Believe it or not, this is easier to do than it sounds.

Let me start with a really simple script that does hardly anything. I will then build on this script as I introduce more concepts. Here is the script:

Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
}

RunOn-Windows

This script creates a function called RunOn-Windows. As I previously noted, the function doesn't really do much. It simply displays a message indicating that the script is running on a Windows machine. The script's body is equally simple. It contains a single command, which calls the function.

As it is written, this script has no idea if it is running on a Windows machine or not. But what if it were possible to perform a test to determine whether the script is running on Windows, and call the function only if the Windows operating system is detected? It's not that hard to do, but you will need to make sure that you are running PowerShell version 6 or higher. Otherwise, this technique won't work.

Microsoft provides a series of variables that you can use to see what operating system is being used. If you want to see if a script is running on Windows, for instance, you can use the $IsWindows variable. This variable is set to True if the system is Windows-based, or False if the operating system is something else. We can easily evaluate the variable as a part of the function call. Here is what the script might look like:

Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
}

If ($IsWindows) {RunOn-Windows}

As you can see, I haven't touched the function itself. What I have done, however, reconstruct the function call so that it only executes if the $IsWindows variable is true. This is a great way to prevent Windows code from running on a non-Windows system.

But what if you want to inform users of non-Windows systems that the script only works on Windows? The only thing we have to do is add an Else statement. Here is what such a statement might look like:

If ($IsWindows) {RunOn-Windows} else {Write-Host 'This script only works on Windows computers'}

This line of code checks if the script is running on Windows. If it is running on Windows, then it calls the RunOn-Windows function. If the script is running on another platform, it displays a message indicating that the script only works on Windows systems.

As handy as this technique might be for preventing a script from running on an unintended platform, it can actually be extended to make a script multiplatform. Here is what such a script might look like:

Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
}

Function RunOn-Linux
{
Write-Host 'The Script is Running on a Linux Machine'
}

Function RunOn-Mac
{
Write-Host 'The Script is Running on a Mac'
}

If ($IsWindows) {RunOn-Windows}
If ($IsLinux) {RunOn-Linux}
If ($IsMacOS) {RunOn-Mac}

This script contains three different functions. Remember, a function never executes unless it is explicitly called. One function contains code that is intended for use on a Windows machine, while the other functions contain code for Mac and Linux.

The script's main body contains three If statements. If the script is found to be running on a Windows machine, it calls a function containing a Windows version of the code. If the script is running on a Linux machine, it calls a function containing Linux-specific commands. Likewise, if the script is running on a Mac, then the script will call a function containing code that is intended for use on a Mac.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

comments powered by Disqus

Subscribe on YouTube