How to Run PowerShell from Python
In this article we will look at how you can run PowerShell scripts from your python scripts.
I recently had to run a PowerShell script from my own python script and found some interesting information online on how to solve the problem.
In order to enforce the learnings when I do something new I like to write about it, so in this guide we will go through some examples similar to my own use case, and also some other examples just to highlight what is possible.
Problem
Sometimes you might like to run PowerShell scripts from your python scripts, why? There are a multitude of reasons but the most likely is perhaps you can’t or don’t want to use a python library which could access information you would like, for example system information on windows.
You know you can solve this with PowerShell so if you can call PowerShell scripts from your python code, this will do what you need.
Solution
The key to all this is the python subprocess
. This module replacesos.system
and os.spawn*
here is what the docs have to say about it:
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
You can check out the documentation for further information, but below are some example use cases to get you started.
Example 1: Running PowerShell from Python to Obtain Operating System Information
In this example, we are using the python subprocess module and the built in PowerShell Get-ComputerInfo
cmdlet which was introduce in PowerShell 5.1.
This simple example will grab the name of your operating system, and then as shown we can take that result and use it in our python code.
In this example, we just print the name of the operating system, but you can see how you could simply do whatever you want with the results from the command.
import subprocess;
process=subprocess.Popen(["powershell","Get-ComputerInfo | select -ExpandProperty OsName"],stdout=subprocess.PIPE);
result=process.communicate()[0]
print ("Our current operating system is:- "+result.decode('utf-8'))
> Output:- Our current operating system is:- Microsoft Windows 11 Home
Example 2: Spawning applications from python
As you can see in this example, we can spawn any application from python using subprocess Popen
that we could run from PowerShell.
So, here are 2 common examples that we like to use, opening notepad and opening calculator from python using PowerShell.
import subprocess;
process=subprocess.Popen(["powershell","calc.exe"]);
result=process.communicate()[0]
print (result)
import subprocess;
process=subprocess.Popen(["powershell","notepad.exe"]);
result=process.communicate()[0]
print (result)
So it’s pretty obvious when you read the PowerShell command what each of these examples is doing. The first one would run the windows calculator program, and the second runs windows notepad.
If the program opens, there is no output, but if it did fail to open then you would get the PowerShell output of the issue.
Conclusion
On the surface of it, this information might not seem that significant, but in reality, depending what you do with it, it could be very powerful.
Imagine all the things you can do from PowerShell and then being able to wrap that in python maybe with a web front end? One possible example which is pretty exciting would be to create a web gui with some variables that let me deploy sql server installations using PowerShell.
There are also many popular PowerShell modules you could call from python like dbatools. Using other PowerShell modules would really extend the usefulness of being able to call PowerShell scripts from python.