Comparing PowerShell and Python
Language Duel
Admins rely on scripts to automate regular processes. In Windows, some configurations are only possible in PowerShell. However, PowerShell is not the only scripting language – Python is also a popular option. In this article, we compare the two languages and examine possible usage scenarios on Windows, Linux, and Mac OS.
We'll start with Python. Linux admins and users have probably used Python, even if only to run a .py
script on their computers. Python, which was developed in the spirit of free software, is available free of charge as source code and in binary form, together with the standard library [1].
Python is an extensive interpretive programming language that goes far beyond a normal scripting language, supporting all of the features and capabilities you expect from modern programming and scripting languages. These features include, among other things, object orientation (including the use of classes and abstract data types), the option of splitting programs arbitrarily into modules (as well as the multitude of existing standard modules), the use of exception conditions (exceptions), and the option of expanding the language (e.g., with code written in C). Unlike many interpreted languages, variable and argument declarations are not necessary, which significantly simplifies rapid programming, but it can also lead to problems (e.g., from inadvertent typos in variable identifiers).
Many users point out that Python code is generally quite readable, which significantly increases its recyclability. Unlike other programming languages, which sometimes make excessive use of brackets and semicolons – and where the resulting errors in translation or interpretation have driven some developers to despair – Python indents lines to add structure. After a familiarization period, even C programmers can see the advantages of this type of representation. Finally, Python is also available on a number of different operating systems.
Differences Between Python 2 and 3
Python is part of the standard scope of Linux and Mac OS operating systems. However, while the most recent Linux versions offer both current and legacy versions of Python (Figure 1), the latest version on Mac OS (Mac OS 10.12 Sierra) only offers the legacy version 2.7.10. If in doubt, you should download and install the latest version from the Python website [1]. Windows users must download and install Python before they can use the scripting language. That said, the following questions remain: Which Python should you choose and how do they differ?
Basically, the difference (based on information from the Python website and various other Python sites) is very simple: All Python 2.x versions belong to Python's legacy branch. In mid-2010, the last major release appeared as version number 2.7. Since then, changes have been introduced, but these are mainly intended to allow for backward compatibility. When this article was written in November 2016, 2.7.12 was the current version number, which we used on both Windows 10 and Ubuntu 16.10. The end-of-life for Python 2 is 2020.
The Python 3 branch, which first appeared in 2008, is under active development and, at the present time, has reached version 3.5.2; it is also available for Windows and Linux systems. Scripts that are written in this version are no longer backward compatible with the 2.x versions. However, some features that were originally developed for the 3.x branch were backported into version 2.6. If you want to port code from Python 2.7 to Python 3.5 the overhead is no longer too big.
Using an example, we'll illustrate some differences between the two versions. If you are working with Python 3, in addition to other features, the following changes will quickly become apparent:
print
is now provided as a function.- The
input
function has been renewed. - The division operator now returns a float value (rather than an integer, as in Python 2).
- The modules have been restructured.
To illustrate just these points, Listing 1 is written in Python 2 and can be executed seamlessly by its interpreter, whereas, under Python 3, it produces errors. However the 5/6
division under Python 2 consistently outputs 0
, because the operator returns an integer value. The corresponding code is shown in Figure 2.
Such an example can easily be migrated manually; for example, the print
function is used instead of the print
command. However, migration is easier with the 2to3
script that comes with Python 3. The script examines the code and replaces the appropriate passages, so that larger scripts can be ported faster. You can also decide whether you want to have the file rewritten immediately or just examined. It also works with modules, as shown in Figure 3. As another example, in Python 2, the input
function examined the value entered and then automatically assigned it to the corresponding type's variable. This is one reason why raw_input()
was often used. In Python 3, the input
function always returns the value entered as a string variable (type str
), which is why this function was replaced by the 2to3
tool.
Listing 1
Sample Code Used in
import urllib2 def hello(name=None): if not name: name = raw_input("What is your name? >> ") print "Welcome to Python, {}!".format(name) print '5/6 is {}'.format(5/6) if __name__ == '__main__': hello() req = urllib2.Request('https://www.python.org/') response = urllib2.urlopen(req) page = response.read() print "and now to the www.python.org page" print page
These are just a few examples, but they clearly show how to chose between Python 2 and 3: If you are new to Python, you should start with Python 3 – many things have been improved and more thoughtfully designed. If you want to use old code or specific packages and libraries that are still based on Python 2, you should, of course, use this version, especially in those cases where porting would be complex. Exploring old Python 2 code with tools such as 2to3
and porting, if necessary, is a good place to start.
No More Windows Without PowerShell
You don't have to be a Windows fan to know that Microsoft sees PowerShell as the central interface at the command-line for all Windows systems. PowerShell has been delivered with the operating system since Windows 8, and Microsoft has steadily pushed integration so that some products, such as Exchange Server and cloud applications centering on Azure and Office 365, are supported by a steadily increasing number of PowerShell cmdlets. The Windows command line, which is known as the command prompt, still features prominently in the Windows 10 Power menu (press the Windows key+X or right-click on the Windows icon). However, the command prompt can now be replaced by PowerShell, which you can find in the system settings. As of Windows 10 Insider Preview Build 14971.1000, Microsoft has changed the default so that PowerShell is called as the default command line in the Power menu.
Just like Python, PowerShell interpretively processes the code. Also in common with Python is object orientation. One of the specific strengths of PowerShell is the ability to process and link commands using pipes. Although classic Unix shells also support this technique, the use of this mechanism in PowerShell in an object-oriented way opens up new possibilities. For example, a cmdlet such as Get-Service
, which creates a list of Windows services, returns the services it finds as objects. If this output is redirected into a pipe, these objects are transferred directly to the cmdlet at the other end of the pipe:
Get-Service | Get-Member
By using the Get-Member
cmdlet, the details of the Member data of the detected services are then listed. If you ever have had to sort the output of a text file on a Unix system using tools such as awk
or sed
and then tried to conjure up ordered output onto the screen, you will quickly appreciate this method. There can be no doubting the great strength of PowerShell: With its help, IT professionals can automate administration tasks quickly on their Windows systems. The possibilities go far beyond the usual scripting solutions for processing files and folders. This is shown, for example, by the integrated Desired State Configuration (DSC) management system for server administration and configuration, which became available in PowerShell version 4. PowerShell has many properties of an object-oriented programming language, offering standardized syntax – even if it takes some getting used to for beginners – as well as close cooperation with the operating system, coupled with good processing speed.
PowerShell Under Linux
Microsoft, which is also increasingly thinking about cross-platform solutions and applications, has only recently opened up to Linux. A version of PowerShell for Linux [2] is available, and, at the time we wrote this article, it was still at alpha version (v. 6.0.0-alpha13), which supported Ubuntu 14.04/16.04, CentOS 7.1, and Mac OS X 10.11, as well as Windows versions. Since then, PowerShellCore v6.0.0-beta7 has been released. Our attempt to install it on Ubuntu 16.10 initially failed, because the installation package was missing the libicu55
library, which the Ubuntu developers replaced with libicu57
in the new operating system version. Fortunately, we were able to download the libicu55
library and install it on our system. Then, installing PowerShell was no longer a problem. In an alpha version, such obstacles are certainly acceptable. However, one hopes such difficulties disappear in the more advanced versions.
After a successful installation, PowerShell on Linux then proved to be remarkably stable on the Linux platform. PowerShell's help pages were easily installed and provided the desired information. Of course, you cannot expect the variety of cmdlets offered on the various Windows platforms and applications, such as Exchange. However, all the basic functions, such as working with files and directories, work perfectly (Figure 4). That said, a range of Shell interpreters, such as the Bash shell, are already available for Linux users, even if they do not offer an object-oriented approach. PowerShell on Linux currently is no more than a successful proof of concept – much like the integration of a Linux shell, including a Linux subsystem, on the Windows 10 Insider Preview version. Consequently, it should not be used for everyday work and administration.
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.