Updates to the Windows Subsystem for Linux, along with PowerShell Core, are set to shake up PowerShell vs. Bash comparisons.
Microsoft partnered with Linux vendor Canonical Ltd. to port Bash (Bourne Again Shell) to Windows in 2016. Bash integration with the Windows environment enables users to forgo dual-booting with Canonical's Ubuntu OS to get native Linux capabilities. Script-savvy Windows admins might wonder if Bash on Windows replaces PowerShell, which is similar to Unix and Linux systems and also already provides OpenSSH connectivity over the Secure Shell protocol.
Windows Subsystem for Linux (WSL) has been around for several years, and developers have adopted it rapidly. Improvements to WSL make it easier to install multiple Linux distributions as necessary, support Linux line endings in Notepad and launch a Linux shell from File Explorer. In the past, admins could install Cygwin Bash to use the shell within Windows, but that did not offer a native Bash experience or proper integration with Windows. As such, WSL offers the best of both worlds and provides an improved experience.
Windows 10 version 1903 further enhances and expands WSL, with the ability, as one example, to navigate the Linux file system within File Explorer. These updates are part of WSL 2, an updated WSL architecture with a real Linux kernel that runs within Windows. WSL 2 enables full system call compatibility, so not only are WSL and Windows tightly integrated, but system performance is higher, as well.
Differences between Bash and PowerShell
Purpose and scope. PowerShell is a configuration management tool that brings the capabilities of Linux command-line interface (CLI) control into the historically point-and-click Windows environment to manage Windows servers efficiently in virtual deployments. Administrators can manage Windows server workloads or host production Linux workloads and server applications via PowerShell.
Bash, on the other hand, is more traditionally suited for development environments. It was introduced to complement and strengthen CLI-based interaction. With the addition of Bash to Windows, code that developers or infrastructure engineers write for Linux works on their Windows systems, too. Picture Linux-first tools -- Python, Ruby, Git -- that are common in DevOps shops running directly on Windows.
Syntax. PowerShell is not just a shell; it is a complete scripting environment. PowerShell invokes lightweight commands called cmdlets at run-time via automated scripts or APIs. While PowerShell does not call for them, old disk OS commands still work well. PowerShell uses aliases, which point old commands to the corresponding new ones. The Get-Alias cmdlet gives you a list of all aliases in PowerShell.
The commands ls -la in Bash and dir in PowerShell are two separate CLI concepts, but the output is not wildly different. Figure 1 shows the output of PowerShell dir commands, and how a directory list displays in PowerShell. The output is in the form of file objects with properties, such as date created and size, listed beside the files.
By contrast, the Bash output in Figure 2 is in the form of a set of strings, which are the text representations of file names. The end result is especially important: The scripts you write take the data that is returned and pass it on to another function or use it to perform an action.
PowerShell relies on an object pipeline. It pipes objects, passing along the output of one cmdlet as the input for another one. The same data can be manipulated with multiple cmdlets in sequence. By piping objects, PowerShell scripts share complex data, passing entire data structures between commands. Bash, on the other hand, passes output and input as plain text, which means it is easy for the user to move information to the next program.
The PowerShell output in Figure 3 demonstrates how one directory entry contains a wealth of properties. Use the command Get-ChildItem | Select-Object * -First 1 to show the many available properties on a single file-system object.
Capabilities. PowerShell enables admins to edit the registry, manage Microsoft Azure cloud and Exchange email or conduct Windows Management Instrumentation. The Bash shell and command language don't offer these capabilities in Windows. As a developer tool on Windows, however, users can code and build functions or services while working on the same files from both the Linux and Windows CLI.
PowerShell makes it easy to access registry values and file properties using a common syntax. XML processing is also straightforward.
The value of PowerShell vs. Bash comes down to the user. If you're working on several Windows systems, Bash is of little use; you'll need PowerShell to write scripts. Admins can't access local Linux files with Windows apps -- such as Windows Notepad -- via Bash.
Learn how to use the PowerShell pipeline with author Adam Bertram.
While Bash is great to manage text files in a scripting environment, everything is managed through APIs, not files. So, Bash is useful primarily to import Linux code to Windows machines and develop that code.
To manage Windows workloads, PowerShell is effective with its .NET framework and COM+ integration. There are many object-oriented and modular features installed with PowerShell that extend the functionality of the tool to manage Windows-centric tasks on Active Directory, Exchange Server and SQL Server. VMware is managed easily via PowerShell as well. The CLI has good tracing and debugging functions. Users have multiple integrated development environments to code and test programs.
Formerly, PowerShell could not compete with Bash on Linux. Bash boasts an ecosystem of tools built from the ground up with Linux in mind. With the advent of PowerShell Core, however, all of that has changed. PowerShell can run as a primary shell within Linux that contains all of the object properties for which PowerShell is famous.
Learning PowerShell and Bash
PowerShell deals with a lot of scripting. If you're from a Unix/Linux background, this tool looks familiar. However, for Windows GUI adherents, there's a steep learning curve. PowerShell Core, currently at version 6 and nearing version 7 at the time of updated publication, contains hundreds of cmdlets, each with a variety of parameters and the unique ability to run on different OSes.
Bash on Windows comes with less than 40 internal functions and around 100 helper programs. With a slimmer syntax, Bash is faster, but PowerShell has the advantage of a consistent syntax structure. If you're just starting out, it will take some time to thoroughly exploit PowerShell's reach. Users familiar with the tool deploy, manage and repair hundreds of systems from any remote location, automate runbooks and use C#-based PowerShell script files to automate repetitive tasks.
Is Windows Bash a security risk?
Cross-OS admins stock up on skills, tools
Microsoft patches Linux subsystem for Windows