How to Use PowerShell with Python
PowerShell is an amazing tool to automate tasks in Windows, whether that is for administrative or personal tasks. However, what happens when you have to use the same script on different machines but don’t have access to Active Directory or other management platforms? While there are many tools out there, Python has been my choice for a while to run my different PowerShell scripts via a tool I created! Let’s dive in to how this is possible!
🔍 Overview: What is Python and PowerShell?
PowerShell is a powerful scripting language for Windows operating systems. This language allows you to make direct changes to nearly everything inside of the system (given that you have th proper permissions). Python, on the other hand, is a versatile general-purpose programming language. It is cross-platform, this means that the python code itself is platform-agnostic. However, we can combine these two. This unlocks a lot of potential for Windows automation, system administration, and even DevOps workflows!
🛠️ Step 1: Install Python and pip
Make sure Python is installed and available in your system PATH.
python --version
pip --version
If either command fails, install Python and enable “Add to PATH” during setup.
💻 A Basic Powershell and Python Program
Before we dive into the code, what module in Python handles PowerShell commands? That would be subprocess.
subprocess allows us to create different commands and run them with a system subprocess. Utilizing the code below, you can see that we give it the name of the subprocess as well as the command itself within a Python list.
# Simple Powershell Script Python Program
import subprocess
command = ['powershell', '-Command', 'Get-Date']
subprocess.run(command)
If you run that directly, then you can see that the powershell command actually ran! Now, I know that is super basic. To really get the best out of this, it would be a good idea to use this for many scripts at once. Take a look at the example below:
# Multiple Powershell Scripts in a Python Program
import subprocess
commandOne = ["powershell", "-Command", "Get-Date"]
commandTwo = ["powershell", "-Command", "Get-Process"]
commandThree = ["powershell", "-Command", "Get-ComputerInfo"]
subprocess.run(commandOne)
subprocess.run(commandTwo)
subprocess.run(commandThree)
This allows us to run all three at once, one after another. This isn’t the most Pythonic way of going about this, so for a more complete example of the same code check out the program below utilizing a loop to iterate through all of the commands that we store in a list:
# Multiple Powershell Scripts in a Python Program
import subprocess
commandOne = ["powershell", "-Command", "Get-Date"]
commandTwo = ["powershell", "-Command", "Get-Process"]
commandThree = ["powershell", "-Command", "Get-ComputerInfo"]
commandList = [commandOne, commandTwo, commandThree]
for command in commandList:
subprocess.run(command)
🧠 Summary
In conclusion, you can utilize powerful system scripts within Windows to automate so many of your tasks while still using a Python as your base application language. Going forward, if you use modules like tkinter you can create GUI’s where all you need to do is click a button to set up your own computers with a universal configuration.
🥷 Code from this article
You can find the code for this article here on my GitHub!