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.0 # 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.0.tar.gz (125.3 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.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_psscriptanalyzer-0.3.0.tar.gz
  • Upload date:
  • Size: 125.3 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.0.tar.gz
Algorithm Hash digest
SHA256 6367ff9706ef364bbb52eb4a530ae02e41168fa3a7b313ef3331fe1814d4221d
MD5 03af66ec1a1d4b75fb14811852f79780
BLAKE2b-256 67706359ca25ac966ff634f7dad8b65acc251677dfd0bb190c42e8ec96793123

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_psscriptanalyzer-0.3.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_psscriptanalyzer-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 950e21522cd27768cd054274a07baf9fc1a47ad474a0ef12b5c02ee2d1348237
MD5 1fee38473d89ed409753d0290fb87f4c
BLAKE2b-256 0d0a233c17b373dbaabe4190ecec2185fd3759603afc3e95baeb77d0602d0c38

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_psscriptanalyzer-0.3.0-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