Skip to main content

Python wrapper for PowerShell PSScriptAnalyzer - static analysis and formatting for PowerShell scripts

Project description

PyPI version License: MIT Code style: ruff Python Compatibility codecov Codacy Badge docs Release

py-psscriptanalyzer



A Python wrapper for PSScriptAnalyzer - bringing cross-platform PowerShell static analysis and formatting to your projects, including a pre-commit hook and integration with CI/CD tools

What it does

Lints PowerShell files (.ps1, .psm1, .psd1) for code quality issues
Formats PowerShell code automatically
Works everywhere - Windows, macOS, Linux
Zero config - installs PSScriptAnalyzer automatically
GitHub Actions ready - with standard error annotations
CI/CD friendly - perfect for automation pipelines
Pre-commit hooks - catch issues before they hit your repo

Installation

pip install py-psscriptanalyzer

Troubleshooting Installation

macOS/Linux: "externally-managed-environment" Error

If you encounter an "externally-managed-environment" error, you have several options:

  1. Use a virtual environment (recommended):

    python3 -m venv py-psscriptanalyzer-env
    source py-psscriptanalyzer-env/bin/activate  # On Windows: py-psscriptanalyzer-env\Scripts\activate
    pip install py-psscriptanalyzer
    
  2. Use pipx (for command-line use):

    # Install pipx if not already installed
    brew install pipx  # macOS
    # sudo apt install pipx  # Ubuntu/Debian
    
    # Install py-psscriptanalyzer with pipx
    pipx install py-psscriptanalyzer
    
  3. Install to user directory:

    pip install --user py-psscriptanalyzer
    

Note: If you're using the package in a CI/CD environment, virtual environments or containers are typically the best approach.

Usage

Command Line

# Analyze PowerShell files
py-psscriptanalyzer script.ps1 module.psm1

# Format PowerShell files
py-psscriptanalyzer --format script.ps1

# Set severity level (hierarchical: Information > Warning > Error)
py-psscriptanalyzer --severity Error script.ps1        # Only errors
py-psscriptanalyzer --severity Warning script.ps1      # Warnings + errors (default)
py-psscriptanalyzer --severity Information script.ps1  # All issues

# Search recursively for PowerShell files
py-psscriptanalyzer --recursive

# Use environment variable for default severity
export SEVERITY_LEVEL=Error
py-psscriptanalyzer script.ps1  # Uses Error level

Python API

from py_psscriptanalyzer import run_script_analyzer, find_powershell

# Find PowerShell installation
powershell_cmd = find_powershell()

# Analyze files
exit_code = run_script_analyzer(
    powershell_cmd,
    ["script.ps1"],
    severity="Warning"
)

# Format files
exit_code = run_script_analyzer(
    powershell_cmd,
    ["script.ps1"],
    format_files=True
)

Pre-commit Hooks

Add this to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/thetestlabs/py-psscriptanalyzer
    rev: v0.3.1 # Use the latest version
    hooks:
      - id: py-psscriptanalyzer # Lint your PowerShell
      - id: py-psscriptanalyzer-format # Format your PowerShell

GitHub Actions

- name: Analyze PowerShell
  run: |
    pip install py-psscriptanalyzer
    py-psscriptanalyzer --severity Error **/*.ps1

Prerequisites

  • Python 3.9+
  • PowerShell (any version - we'll find it!)

Need PowerShell? Get it here:

Documentation

Full documentation available at py-psscriptanalyzer.thetestlabs.io

Contributing

Contributions welcome! See our development guide in the documentation.

License

MIT - see LICENSE file.

A tool for linting and formatting PowerShell code using PSScriptAnalyzer.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_psscriptanalyzer-0.3.1.tar.gz (126.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_psscriptanalyzer-0.3.1-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file py_psscriptanalyzer-0.3.1.tar.gz.

File metadata

  • Download URL: py_psscriptanalyzer-0.3.1.tar.gz
  • Upload date:
  • Size: 126.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for py_psscriptanalyzer-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f45a1f8328a1e6189aaaa992648eecf031111d3eb611e341a7d949a4422869c5
MD5 a6a720d486937785be145efe0c10a132
BLAKE2b-256 8f80744f23a269c194ee33a1176c95b9ad38fc965773de79c5f596b4e42dd63e

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_psscriptanalyzer-0.3.1.tar.gz:

Publisher: release.yaml on thetestlabs/py-psscriptanalyzer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_psscriptanalyzer-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for py_psscriptanalyzer-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50dbbfe6e307610c92af65d9127cd9722ea4118b3cba563aeea303edcb7e2035
MD5 612557f722017cfdb2beafde95565e50
BLAKE2b-256 839d79fdb86e6d874bcf2c980f954f1a7404c11461f5295820029fee8cc4753a

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_psscriptanalyzer-0.3.1-py3-none-any.whl:

Publisher: release.yaml on thetestlabs/py-psscriptanalyzer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page