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.2.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.2.0.tar.gz (115.1 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.2.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_psscriptanalyzer-0.2.0.tar.gz
  • Upload date:
  • Size: 115.1 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.2.0.tar.gz
Algorithm Hash digest
SHA256 928164200dfd7c082681e48b08cedf38d0aecd52e0f8976161c8aeb9e964d8a3
MD5 4e1d8cbf7ed8b566fd3ca468d4d4b5ae
BLAKE2b-256 ec6cd969e03760c83da0d850a478adb6c66e0663f75aa74e3411190cabab30b9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for py_psscriptanalyzer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ea4f12e6ca146b12caab779592908d7810301a9be8cb3a3e399307bc1b4ff52
MD5 a1a73cbd9c8218be6b0fb2f56ab5ebc8
BLAKE2b-256 494db62f8be1da9f86f263f67da7bc4935f76ebe59bd84362a0a1452211e149c

See more details on using hashes here.

Provenance

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