Python wrapper for PowerShell PSScriptAnalyzer - static analysis and formatting for PowerShell scripts
Project description
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:
-
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
-
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
-
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:
- Windows: Already installed, or get PowerShell Core
- macOS:
brew install powershell - Linux: Installation guide
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f45a1f8328a1e6189aaaa992648eecf031111d3eb611e341a7d949a4422869c5
|
|
| MD5 |
a6a720d486937785be145efe0c10a132
|
|
| BLAKE2b-256 |
8f80744f23a269c194ee33a1176c95b9ad38fc965773de79c5f596b4e42dd63e
|
Provenance
The following attestation bundles were made for py_psscriptanalyzer-0.3.1.tar.gz:
Publisher:
release.yaml on thetestlabs/py-psscriptanalyzer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_psscriptanalyzer-0.3.1.tar.gz -
Subject digest:
f45a1f8328a1e6189aaaa992648eecf031111d3eb611e341a7d949a4422869c5 - Sigstore transparency entry: 394872507
- Sigstore integration time:
-
Permalink:
thetestlabs/py-psscriptanalyzer@6145f50af9b774b416f239eb5107a42c8c368040 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/thetestlabs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@6145f50af9b774b416f239eb5107a42c8c368040 -
Trigger Event:
push
-
Statement type:
File details
Details for the file py_psscriptanalyzer-0.3.1-py3-none-any.whl.
File metadata
- Download URL: py_psscriptanalyzer-0.3.1-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50dbbfe6e307610c92af65d9127cd9722ea4118b3cba563aeea303edcb7e2035
|
|
| MD5 |
612557f722017cfdb2beafde95565e50
|
|
| BLAKE2b-256 |
839d79fdb86e6d874bcf2c980f954f1a7404c11461f5295820029fee8cc4753a
|
Provenance
The following attestation bundles were made for py_psscriptanalyzer-0.3.1-py3-none-any.whl:
Publisher:
release.yaml on thetestlabs/py-psscriptanalyzer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_psscriptanalyzer-0.3.1-py3-none-any.whl -
Subject digest:
50dbbfe6e307610c92af65d9127cd9722ea4118b3cba563aeea303edcb7e2035 - Sigstore transparency entry: 394872527
- Sigstore integration time:
-
Permalink:
thetestlabs/py-psscriptanalyzer@6145f50af9b774b416f239eb5107a42c8c368040 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/thetestlabs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@6145f50af9b774b416f239eb5107a42c8c368040 -
Trigger Event:
push
-
Statement type: