You can run a PowerShell script in Python using the subprocess or os module. In this article, we will learn how to execute a PowerShell script from Python in Windows, Mac, or Linux. The fundamental prerequisite is to have PowerShell installed on your system.
Windows comes preinstalled with PowerShell (you can update it by downloading the latest version from this link). You can install PowerShell core on Linux and Mac using the information below.
Installing PowerShell core in Linux
You can install PowerShell core on Debian by running the following commands from the terminal (source: Microsoft website). If you are running a different distribution, you can get the installation commands from here.
1 2 3 4 5 6 7 8 |
# Install system components sudo apt update && sudo apt install -y curl gnupg apt-transport-https # Import the public repository GPG keys curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - # Register the Microsoft Product feed sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list' # Install PowerShell sudo apt update && sudo apt install -y powershell |
Installing PowerShell Core on Mac
You can install PowerShell using Homebrew by running the following command on the terminal.
1 |
brew install --cask powershell |
Or update it using the commands
1 2 |
brew update brew upgrade powershell --cask |
Once done with the installation, you can start PowerShell from Windows, Linux, or Mac terminal by running this command in the terminal
1 |
pwsh |
And execute a PowerShell script using the command
1 |
pwsh <path/to/the/script> |
At this point, we are ready to execute a PowerShell script from within a Python script.
Running PowerShell Script Within Python Script
As an example, we will execute the PowerShell script named script1.ps1 with the content below. The script accepts one parameter, $Name, and prints a greeting message to the console based on the time of the day.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
param( $Name ) $noon = Get-Date -Hour 12 -Minute 0 -Second 0 $current_time = Get-Date Write-Host "The time is $current_time" if ($current_time -gt $noon){ Write-Host "Good afternoon, $Name`!" } else { Wite-Host "Good morning, $Name`!" } Then we can execute <em>script1.ps1</em> from Python code using the code below.<br><br>import subprocess, sys # Running "script1.ps1" script with the -Name parameter set to "Decker". p = subprocess.Popen(["pwsh", "script1.ps1", "-Name", "Decker"], stdout=sys.stdout) # Run this to send the result to stdout. Finish the execution process, in short. p.communicate() |
Output:
The time is 04/14/2023 18:46:50 Good afternoon, Decker!
If you want to save the output of the PowerShell script to a variable, you can send the output to the subprocess.PIPE, as shown below.
1 2 3 4 5 6 7 8 |
import subprocess, sys out = subprocess.Popen(["pwsh", "script1.ps1", "-Name", "Decker"], stdout=subprocess.PIPE) # read the content of the standard output (stdout) and decode it # to a string and save the results into "output" variable output = out.stdout.read().decode("utf-8") print(output) |
Output:
The time is 04/15/2023 17:37:35 Good afternoon, Decker!
You can also run the PowerShell script using the os module as follows.
1 2 3 4 |
import os return_code = os.system("pwsh script1.ps1 -Name Decker") print(return_code) |
If the execution runs successfully with no error, return_code=0. Note that os. system() has limited functionalities. The subprocess module discussed above provides more facilities for spawning new processes.
Conclusion
This guide discussed using the subprocess module to run the PowerShell script using Python across different platforms – Windows, Linux, and Mac.