Running Python in PowerShell Universal

PowerShell Universal Python

October 10, 2023

quote Discuss this Article

PowerShell Universal has historically been a PowerShell only platform. With the release of PowerShell Universal 4.2, we’ve will be adding the ability to run Python scripts. This post will walk through how to do so with the latest nightly builds of 4.2.

This functionality will ship in November as part of PowerShell Universal 4.2.

Minimal Environments

PowerShell Universal v4.2 will introduce a new setting for a environments referred to as Minimal. Minimal environments do not attempt to load Universal assemblies or establish a gRPC connection between the child process and the parent process. This allows for PowerShell Universal to run in environments that are not based on .NET. Minimal environments take advantage of STDOUT and STDERR to collect output from the process started in the environment. This can also be helpful for PowerShell scripts that may load modules that conflict with Universal assemblies.

Python Environment

To configure a Python environment, we would first create a new environment and set the path to python.exe. This requires that Python is installed on the machine. Next, set the command line to the following string: {scriptPath}. This value will be expanded to the path of the script that is being executed and passed in at runtime.

While it was technically possible to run Python scripts in previous versions of PowerShell Universal, this new environment type will benefit from running Python directly and not starting an intermediate PowerShell process that then executes Python. This results in a much faster startup time for these jobs.

Running Python Scripts

Next, create a script with a .py extension. We’ve updated the admin console to correctly identify Python scripts and syntax highlight them properly. Finally, click run and select your Python environment.

What’s next for Minimal Environments and Python integration?

Minimal environments will allow you to run whatever process you’d like from the script execution engine and pass in the script path as an argument. Being that we replace that {scriptPath} macro, you can include other arguments as well. This could be useful for other scripting languages, like bash on Linux. Minimal environments do not work with APIs, Apps or Terminals. These features require a gRPC implementation that is not available in these other languages and engines. We do hope to expose our gRPC proto files during the v5 release time frame. This would allow for the generation of clients that could run in any language where gRPC and protobuf are supported.