PowerShell to Python and Back

November 05, 2017 Security and Compliance, MOVEit

Adam the Automator describes taking the leap from being a PowerShell guy to learning a new scripting language in Python.

If you’ve read any of my previous articles, you will understand that I’m a huge PowerShell guy. I’ve been working with PowerShell for nearly ten years now and with the latest 2 of those years have been primarily a PowerShell developer writing PowerShell code full-time.

I love PowerShell and automation, and if you’re in charge of automating any kind of Microsoft product, you probably know a thing or two about PowerShell too. But, PowerShell is not the only game in town; enter Python.

Beyond PowerShell

People inside of the Microsoft community (which I’m a part of) love PowerShell and use it every day, but there’s an entirely new world out there in open-source land. The Linux world is also a thriving community full of great operating systems, tools and products to manage things. In Linux land, when it comes to scripting, PowerShell is the weakling on the block. The big boys of scripting in Linux are Python and Perl. Although PowerShell and these languages are all technically cross platform, it’s clear that each language has its dominant audience.

Related: A Newbie’s Guide To Python Functions

In the next few articles in this series, I will be taking you on my personal journey being a PowerShell “expert” to being completely new at another language; Python, in this example. Why Python? I’m choosing Python because it’s the fastest growing language. It’s a programming language that looks and acts like a scripting language. Unlike lower level languages like C, C++, Java and others, to me, it doesn’t feel like “coding”. It feels like I’m in PowerShell scripting away. From someone that still has a hard time defining myself as a “developer”, this is critical. I’m not concerned with how to make the most efficient use of memory and CPU. I don’t care about pointers, registers and everything under the hood. I just want to automate! Python seems to be the best choice.

 

Why Python?

In the short time I’ve been using Python, I’ve discovered that there are many principles that easily carry over from one language to another. For example, nearly all programming and “scripting” languages have constructs like if statements, loops, variables, functions, objects, etc. Understanding the basic foundation of code and how to write general code will give you a major leg up in learning a new language. Once you can grasp these basic concepts, this knowledge will transfer over to lots of other languages, including Python.

Differences Between Python and PowerShell

Once you’ve got the basic concepts of code down, the next step is to learn the “best practice” way to perform a task in that language. For example, in PowerShell, it’s common to perform tests up front on function parameters via parameter validation. I’ve recently learned that in Python, no validation is typically performed. Instead, coders enclose the code inside of a try/catch block, catch the exception and deal with it then. This validation is just one, tiny example of what “Pythonistas” call “pythonic” code.

When I started with PowerShell, I was coming from a primarily PHP background. I was ending lines with semicolons, using PowerShell aliases like echo to send output to the console and so on. Even though you can make your new language of choice look like another language you’re used to doesn’t mean you should. It’s important to not only learn the syntax but also how to write the code according to agreed upon guidelines in the community.

Stay tuned here on the IpSwitch blog for more articles on Python from this hardcore PowerShell guy in the quest to call myself multilingual!

 

Adam Bertram

Adam Bertram is a 25+ year IT veteran and an experienced online business professional. He’s a successful blogger, consultant, 6x Microsoft MVP, trainer, published author and freelance writer for dozens of publications. For how-to tech tutorials, catch up with Adam at adamtheautomator.com, connect on LinkedIn or follow him on X at @adbertram.

Read next Leveraging PowerShell Automation in the Cloud