PowerShell vs Bash: What's the Difference?
If you've followed IT trends for any length of time, you've no doubt have come across a few Windows vs. Linux articles. However, pitting these two OSes against one another is not always logical or fair. In most cases it really is an apples and oranges comparison, based on the tasks that are being measured.
Windows and Linux are both very capable operating systems, and each has its own myriad pros and cons that we can debate. But how often do you stop to think about the scripting and automation potential of the two OSes? We decided to look at some of the things that you can do with the scripting functions that ship with Windows and Linux.
A Little History: PowerShell
Windows was never the greatest at automation tasks. This was partially due to the way that it implemented its command line. It was very much a throwback to the familiar DOS prompt that launched Microsoft to being the massive success it is today. But it lacked features when compared to Unix and later Linux systems.
PowerShell is Microsoft's automation and task framework — it is good for configuration management. PowerShell relies on components called cmdlets that are built into PowerShell. Additional functionality is available via modules. These are installed from the PowerShell Gallery directly from the command line.
PowerShell is different from Bash because it is designed to interact with .NET structures natively in Windows. This means that it can pipe objects and data between scripts, applications and sessions. Each object has its own series of properties, which makes the data handling within PowerShell even more granular. Data can be specified as numbers (integers), words (strings), Boolean (true and false), and many other types. This means that you can get really specific with the way your scripts deal with data input and output.
A Little History: Bash
Linux and Unix systems have always benefited from being structured through a multi-user terminal environment. This means that you can launch additional sessions on the same system, and run scripts and applications without affecting the main sessions that other users are logged in as. This was very different from early Windows and DOS systems that were single user, single session environments until Windows NT came around in the mid 90s.
The original shell that shipped with Unix was known as the Bourne shell, named after its creator Stephen Bourne. Bash (Bourne again Shell) is the open source successor to the Bourne shell. Bash was widely adopted when Linux was created in the early 90s, which is why it is still in use today.
Features that make Bash very popular are many, chief among which are system stability and the fact that it is open source. It is found in just about every distribution of Linux because of this. All of these factors make it one of the most used scripting environments for IT professionals today.
When to Use PowerShell
Windows administration has gotten a whole lot easier since PowerShell development became a feature in the Microsoft landscapes. Instead of wrestling with ungainly batch files and the Windows Scheduler, sysadmins have access to a new toolbox of impressive apps and functions.
PowerShell can drill down into fine details to create powerful scripts that work, as well as some commercially available applications. PowerShell can pull data straight from the WMI subsystem, giving you real-time, deep-level information about anything from process IDs and Handle counts.
PowerShell is plugged into the .NET framework, so you can create great looking menus and winforms that look like legitimate applications. You can use PowerShell to do anything from querying SQL Databases to grabbing your favorite RSS Feeds right into your PowerShell session for further manipulation. It is a real Swiss army knife for system administration within Windows environments.
When to use Bash
If you are running Linux systems, then you know about the need for automating tasks. Early tape drives were used for backups with tar archiving. These operations could be scripted in Bash and then run via a cron schedule. We take this kind of thing for granted today, but many tasks had to be completed manually before environments like Bash were created. Anything to do with file manipulation such as archiving, copying, moving, renaming and deleting files are all right up Bash's alley.
More advanced file manipulation is also possible. You can find files created on certain dates, and which files have CHMOD and owner permissions changed. Bash is also great for creating interactive menus for running scripts and performing system functions. These are crude in the sense that they are run in a non-graphical environment, but they work very well. This is great for sharing your libraries of scripts with others. Running a script from a Bash menu is simply a matter of entering a number and hitting the Enter key.
What Have We Learned?
PowerShell and Bash are similar in some ways, but also very different. Here are four key ways they're different.
PowerShell Handles Data Differently
PowerShell is different from Bash in the way it handles data. PowerShell is a scripting language, but it is able to feed data in different formats in a way that makes it feel like a programming language. PowerShell also deals with scopes in its scripts.
Using variables with $session, $script and $cache gives your scripts additional flexibility by allowing the variables to be handed off to other commands in the same script or PowerShell session. When you really get into PowerShell it becomes a tool that you cannot live without.
Bash is a CLI
Bash is a CLI, which stands for Command Language Interpreter. Like PowerShell, Bash is able to pass data between commands via pipes. This data is sent as strings though. This limits some of the things that you can do with the output from your scripts such as mathematical functions.
PowerShell is Both CLI and Language
The default PowerShell Integrated Scripting Environment (ISE) that ships with Windows shows how you can quickly and easily create scripts without sacrificing direct access to a command line. By default, the top section lets you type out your lines of scripting code and quickly test it.
The window below is a PowerShell command line that gives you quick access to run single commands. This gives you the best of both worlds between a scripting language and a command-line shell. The ISE is a great tool to quickly prototype solutions.
PowerShell and Bash are Both Powerful Tools
The environment that you work in will define which tool you choose. Linux systems administrators who script in Bash find it relatively easy to pick up PowerShell scripting. PowerShell scripting skills also translate over to Bash scripting to a certain degree.
The main differences between these two scripting languages are syntax and data handling. If you understand concepts such as variables and functions, then learning either one of these languages starts to make a lot of sense.
Once you start scripting tasks you might be surprised to find yourself addicted to creating tools and automating processes. The key to scripting is understanding the way that your data is handled and how it flows between commands. Once you learn how to harness the power pipelines and how they work is the first step towards understanding automation, which can lead to DevOps and SecDevOps.
How you get there depends on what you script in. PowerShell and Bash are only the beginning, and they are both excellent places to get started.