2024-01-11

Enhancing PowerShell with Python: A Customization Guide

PowerShell, a powerful command-line shell and scripting language, can be extended and customized to work harmoniously with Python.

By leveraging Python tools and libraries, you can enhance your PowerShell experience, automate tasks, and streamline workflows.

Installing and Running Python Scripts Locally

pipx is a package manager for Python applications that allows you to install and run Python applications in isolated environments. It is written in Python and requires Python 3.7 or later to be installed.

For a typical Python/Msys2 installation, as shown here, add to user's $Profile the following lines of code:

    # Vim mode also possible
    Set-PSReadLineOption -EditMode Emacs
    # User's home dir
    $homeDir = $env:USERPROFILE
    $env:PATHEXT += ";.py"
    # typical python.exe/pip.exe dir:
    $env:PATH += ";$homeDir\AppData\Roaming\Python\Python310\Scripts"
    # required by pipx
    $env:PATH += ";$homeDir\.local\bin"
    # git.exe dir, msys2 installation folder 
    $env:PATH += ";C:\path\to\msys2\usr\bin"

ICE COOREL Monitor Stand Riser - 1PCS ABS Material Multi-Functional Desktop Computer Screen Shelf with Foldable Storage Base
As a Temu Affiliate I earn from qualifying purchases

Now, when you reopen the PowerShell, you will be able to install pipx with:

   py -m pip install --user pipx

Suppose you want to use the excellent rich-cli library for beautiful console output. You can install it as an executable with Pipx:

   pipx install rich-cli

Now, as shown in the video below, you can display tabular data from a CSV file with proper formatting with:

   rich.exe "https://tessarinseve.pythonanywhere.com/staticweb/dataset_1000.csv"

Running Remote Python Scripts Locally

Pipx isn't limited to local installations. It can also run remotely hosted Python scripts locally. The script below can help to understand how a Python module performs and where optimizations can be made. If no module name is provided, it lists all installed modules.

    # filename : module_load_time_profiler.py
    import cProfile
    import pstats
    import importlib
    import sys

    def load_module(module_name):
        module = importlib.import_module(module_name)

    if __name__ == '__main__':
        profiler = cProfile.Profile()
        profiler.enable()

        if len(sys.argv) > 1:
            module_name = sys.argv[1]
        else:
            print("The following modules are installed:")
            help('modules')
            sys.exit()

        load_module(module_name)

        profiler.disable()
        stats = pstats.Stats(profiler)
        stats.sort_stats('cumulative')
        stats.print_stats()

This script can be executed from the PowerShell console using the following command:

   pipx run https://tessarinseve.pythonanywhere.com/staticweb/module_load_time_profiler.py module

In this command, replace module with the name of the module for which you'd like to obtain performance statistics.