How and why PowerShell Linux commands differ from Windows – TechTarget

With so many differences between Linux and Windows, it’s incredible that developers can create cross-platform software, especially products that offer similar or identical experiences on different OSes.

While Microsoft strives to make PowerShell look and feel similar on all systems, there are a few important differences in using PowerShell on Linux vs. Windows. Some might be obvious to developers already familiar with the two platforms, but others might come as a surprise.

PowerShell vs. Windows PowerShell

Because this tutorial refers to PowerShell, not Windows PowerShell, it’s important to understand the differences between the two. PowerShell is an open source program built on .NET Core that is compatible with Windows, macOS and Linux OSes. Windows PowerShell is exclusive to Windows — where it is included by default — and is built on the .NET Framework. This tutorial uses the latest version of PowerShell — 7.2.4 at the time of writing — on both Windows and Linux.

.NET Core

.NET Core enables PowerShell to work across OSes. In 2014, Microsoft introduced .NET Core, which made its proprietary .NET software framework compatible with Linux and macOS. This created a path for any software built on the Windows-specific .NET Framework to be built in a cross-platform manner, including PowerShell. While the vast majority of .NET Framework’s core functionalities and features are available in .NET Core, changes have been made and are detailed in Microsoft’s .NET documentation.

Case sensitivity

Of all the differences in PowerShell between platforms, case sensitivity is one of the most significant. As a longtime Windows user, I’ve spent more time than I care to admit troubleshooting a script on Linux only to realize that I used case incorrectly in a file or module name.

Figure 1. Running a script for Test.txt returns True.

While file and module systems are case sensitive, PowerShell users can still tab-complete file names without worrying about case sensitivity.

For example, if there is a file named Test.txt in the current working directory, running a script to test for both test.txt and Test.txt will only return True for Test.txt, as seen in Figure 1. However, typing ./test will tab-complete to Test.txt unless there is another file or folder that starts with test.

File system

Another key difference between Windows and Linux is the file system. Windows uses lettered drives and backslashes as the directory separator, whereas Linux uses a tree structure and forward slashes.

Figure 2. The system will return True when testing for a .Test.txt file on Linux.

PowerShell enables paths to pass to cmdlets using either a forward slash or backslash, interpreting either as a proper directory separator for the current platform. This is useful when writing cross-platform scripts, as it eliminates the need for logic to check for the OS and apply the appropriate directory separator.

To demonstrate, the script shown in Figure 2 returns True when testing for a ……..

Source: https://www.techtarget.com/searchitoperations/tutorial/How-and-why-PowerShell-Linux-commands-differ-from-Windows

Posted on

Leave a Reply

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