How to Python in PowerShell

We see a ton of articles and posts about getting started in PowerShell to Python, but how about from Python to PowerShell? In this blog post, you will get a hands-on approach at Python-ing in PowerShell. We will go over basics of how to run commands and scripts then get into logic and loops.

Prerequisites

The prerequisites for following along with this post are:

  • An understanding of Python from an intermediate to advanced level.
  • Python3 installed which you can find here for any operating system.
  • PowerShell Core installed which you can find here for any operating system.
  • A text editor. For this post, I am using Visual Studio Code (VS Code) version 1.42.1. VS Code can be downloaded here.

The Goal

The goal of this blog post is to take what you know about Python and use it in PowerShell. The phenomenal part about these two languages is they are great for programming and automation scripting on any platform. With that being said, it's very good to know both. In most DevOps roles, you will see the need for someone to know at least one of these languages, but it's typical that knowing both is preferable.

What is PowerShell?

Before writing PowerShell, let's talk about what it is. PowerShell is a scripting language created by Jeffrey Snover and released in November of 2006. PowerShell was created to extend the command-line interface and make easier to use. Before PowerShell, Windows sysadmins and engineers had VB Script and Batch. The idea behind PowerShell was to enhance the way Windows Sysadmins were writing automation code. In the beginning, PowerShell was looked at as a toy language, but not for long.

PowerShell became one of the top scripting and programming languages for Windows professionals. It became so popular, that most of the Windows operating systems we know today wouldn't be able to function without it.

With PowerShell being so popular, there was one problem, it was only on Windows. With PowerShell being closed-source, there was only one way to expand, open-source the language. In 2018 PowerShell Core 6.0 was introduced. PowerShell Core is available on all platforms (Windows, OS X, Linux) and even a default installation option on Ubuntu.

Modules and Libraries

Without libraries in Python, the language wouldn't be very useful. In fact, it wouldn't be operational. The same goes for PowerShell. When a request is being made to a resource (Azure, Active Directory, IIS, etc.) with PowerShell, it's using some module. Think of modules in PowerShell like libraries in Python.

Installing Modules and Libraries

When installing a library in Python, it would look something like the below.

pip install Azure

When installing a module in PowerShell, it would look like this.

Install-Module -Name Azure

Installing the library/module can be done for both languages at the terminal. Once the module/library is installed, it can be used across the environment.

Importing Modules and Libraries

When importing a module in Python, it will look like the below.

import logging

Similarly in PowerShell, you would use the Import-Module cmdlet.

Import-Module Az

In PowerShell, cmdlets are structured in a Verb-Noun method. This means you'll always see cmdlets set up as Get-Something, New-Something, Set-Something, etc.

Running Commands and Scripts

In the previous section, you saw the core functionality of any script - a module or a library. You learned how to install a PowerShell module and import a module. In this section, you'll see how to run commands and scripts.

When running a script in Python, there would be a .py file saved somewhere. Similar in PowerShell, but instead of the file extension being .py, it would be .ps1. Any file that has an extension of .ps1 indicates that it is a PowerShell file. Similarly, if you see a psm1 file extension, a psm1 is for a PowerShell module.

The below screenshot is a file saved with a .py extension.

.PY

Below is a screenshot with a .ps1 extension.

.PS1

The final extension screenshot is for a .psm1

.PSM1

Output

In the previous section, you learned what file extensions are used in PowerShell. Now that you know what file extensions you need to create, it's time to write some code! The first thing you'll take a look at is output.

Output in Python is typically from the print()method. The print method is built into Python and similarly, in PowerShell, there is a built-in cmdlet called Write-Output.

In Python, you would write output in the print() method, run the script, and the script will look similar to the below.

Hello World Python

When creating any output in PowerShell, using the Write-Output cmdlet will look very similar to Python.

Hello World PowerShell

Variables

In the previous section you learned how to run code in PowerShell and create an output. How about when you don't want the output to be static? That's where variables come in.

In Python, a variable will look like the below.

Python variable

In PowerShell, it's the same concept, but the variable looks different because of the $ sign.

PowerShell variable

If you have used bash in the past, PowerShell variables are very similar. In bash when calling a variable, you use a $ sign.

Loops

In the previous section, you took a look at defining variables in PowerShell. Now it's time to take a look at loops using for

In Python, a for loop would look something like the below.

Python For Loop

In PowerShell, for loops look way different. The reason why is because while Python is a white-space based language, PowerShell is a bracket-based language. Whitespacing doesn't matter in PowerShell as long as the code is in the brackets it's supposed to be in.

Below you will see an example of a for loop in PowerShell.

PowerShell For Loop

If you run the above, you'll notice that this will cause an infinite loop, which means the loop will keep running until you manually stop it. When it comes to for loops between Python and PowerShell, as you can see there are some key differences.

Logic

In the previous section you took a look at for loops to loop through a variable. In this section you will look at some logic with if statements.

Similar to the white-space vs bracket-based differences that you saw in the Loops section, you will see a similar trend with if statements.

Below is an if statement in Python.

Python If statement

Because there is an H in the hello variable, the output prints Hi

In PowerShell, the output will be the same, but notice the bracket-based syntax and syntax differences.

PowerShell If statement

There are some key differences between the Python and PowerShell if statement that are very important.

The first one is in Python, the string ('H') would be on the left of the if statement and the variable that you're comparing it to would be on the left. With PowerShell, it's the opposite. The variable is on the left and the string is on the right.

The second difference is if you take a look at the PowerShell string ("H*") you will see a * which means wildcard. That tells PowerShell to search for anything with an H.

The third difference is the comparison operators. With Python, in is used and in PowerShell, -like is used. All operators in PowerShell start with - and then the operator is defined. The -like operator is similar to the * in the sense that it's a wildcard. The -like operator is telling PowerShell to search for anything in the $hello variable with an H*.

Summary

In this blog post, you learned the important core concepts on how to start your PowerShell journey coming from Python. You learned how to install core modules and libraries followed by learning about the different PowerShell extensions. You finished off with seeing output, loops, and logic in the PowerShell code. PowerShell is a great next step from Python. Not only because they are set up very similar, but because both languages round out a DevOps engineer well.


Michael Levan is tech enthusiast and engineer at heart. He is the Chief Engineer and Principal DevOps Consultant at CloudDev.Engineering LLC.

Michael focuses his time on 50% content (blogs, videos, video courses, podcasts, etc.) and 50% consulting. Topics include Azure DevOps, PowerShell, Python, AWS, Infrastructure-as-code, containerization, and many more. He Is utterly fascinated with DevOps and Cloud Engineering. Michael simply can't get enough of all of the cool, new, and exciting technology that's out there.

When he’s not working, he loves spending time with his son and checking out new breweries.

Subscribe to the CloudSkills Weekly Newletter

Get exclusive access to special trainings, updates on industry trends, and tips on how to advance your career in the tech industry.