PowerShell cross-platform best practices 7

Contents

Powershell logo

When PowerShell was first released, the goal was to bring powerful scripting tools to Windows, which usually had a GUI based administration at the time. Since then, PowerShell has grown to become one of the most popular scripting languages.

One of the biggest contributors to this surge in popularity is that the Microsoft PowerShell team made it happen. open source in 2016, Introducing PowerShell Core for PowerShell version 6. Despite this, that caused some unexpected problems, like having to support compatibility between version 5.1 (Windows PowerShell) and the version 6.2 (PowerShell Core), or have the same cmdlets doing slightly different things in different versions of .NET.

To fix that, the PowerShell team announced that they were retiring PowerShell 5.1 and that the next version after 6.2 would PowerShell 7, which would aim to replace Windows PowerShell on Windows operating systems without sacrificing any compatibility when running on Mac or Linux.

PowerShell 7 was GA in march from last year, and even when the cmdlets themselves run on any operating system, there are still some best practices to follow to make sure your PowerShell scripts run smoothly no matter where they are running.

Use lowercase directory names

In Windows operating system, the or the backslash character is used to annotate nested directories in a filesystem, while on Linux and macOS, the / or the forward slash character does it instead and the backslash is used as an escape character. (If at any time you have trouble keeping these two straight, imagine it overturns. The backslash falls back, forward bent bar falls forward)

This can pose some challenges when scripting in most other languages, pero PowerShell 7 will translate to a directory object and don't really care about the characters used to split the path. Despite this, one area you can get into trouble is that Linux and macOS file systems are case sensitive and will treat directory names in different cases as different directories.

Best practice here is to keep directory names lowercase, no matter what route you use. If required, you can pass the path as a string and call the ToLower() on it in your code, but this will only work if the directory name is already lowercase.

  kKeep directory names lowercase, regardless of which route you use.

Do not use aliases

Aliasing can be a great way to shorten the amount of writing you need to do when running PowerShell from a console. It is much easier to write ls O cgi that Get-ChildItem even with the completion of the tab. Having said this, It is rarely a good idea to use aliases in your scripts because it makes the script depend on having that alias set up wherever it runs.

That is especially true for Linux and macOS systems.. On those platforms, instead of using an alias to run another PowerShell command, run the native command, which means that the return object will be very different and that will remove the rest of the script.

ls and Get-ChildItem running on Windows 10 against Ubuntu Linux

ls and Get-ChildItem running on windows 10 vs Ubuntu Linux.

Some commands like ls It may not change too much, but with others like him date command, PowerShell returns a totally different object from the native Linux command, which will remove anything that depends on the output later in the code. Even if you only need the string value in a PowerShell script, use PowerShell to get it.

date returns a DateTime object on Windows and a String object on Linux.

date returns a DateTime object on Windows and a String object on Linux.

Use a change statement to set the operating system

Sometimes, you have a dependency that you cannot script, no matter what kind of tricks I use. In those cases, PowerShell 7 has some reserved brands that will return true O false depending on the operating system from which it is running. As an example, $IsMacOS To return to True on macOS and False on any other operating system, and $IsLinux To return to True no matter what Linux distribution you are on.

In addition there is a $IsWindows flag, but with a big caveat: just return True al ejecutar PowerShell Core o PowerShell 7 in Windows. Never transferred back to Windows PowerShell, so it will only throw an error when run there.

Tyler Leonhardt, from the Microsoft PowerShell team, came up with a great solution to establish what OS you are on when running cross-platform, what is the change statement that describes here.

Excellent solution to determine what operating system you are on when running cross-platform.

Now you should have an idea of ​​some alternatives for writing your PowerShell scripts and modules 7 so they can run a little more seamlessly between platforms. Try them in your scripts today!!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.