<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

PowerShell to Python and Back

Adam Bertram| November 06 2017

| IT insights


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.powershell-to-python.jpg

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.

Enhance your IT career by learning how to automate with Python. Get started  with this free Python guide.

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!

Topics: IT insights

Leave a Reply

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


Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.