PSScriptAnalyzer: A Guide to PowerShell Static Code Analysis

November 24, 2026

Frank Kresse

Listen to this blog post!

The 2026 Microsoft Automation Benchmark

Find out why 72% of teams can’t scale automation safely. And what it takes to fix it.

Get the free report

Table of contents:

The 2026 Microsoft Automation Benchmark
Find out why 72% of teams can’t scale automation safely. And what it takes to fix it.
Get the free report

Introduction

PowerShell scripts often grow faster than you’d expect: a simple script  for day-to-day admin tasks becomes a module used across the entire team. That’s when issues you overlooked while writing the script can start to cause problems: a parameter that never gets used, a plaintext password, or an alias that doesn’t exist on another system.

Static code analysis finds these problems without running the code. For PowerShell, Microsoft’s standard tool is PSScriptAnalyzer. This article shows you what the module can do, how to install and configure it, and how to integrate it with your editor, build pipeline, and ScriptRunner.

What is PSScriptAnalyzer?

PSScriptAnalyzer is an open-source module that checks PowerShell scripts and modules against a set of rules. According to the Microsoft documentation, these rules are based on best practices compiled by the PowerShell team and the community. For each finding, the module reports the rule, severity, line number, and an explanation.

PSScriptAnalyzer runs on Windows PowerShell 5.1 and PowerShell 7 and can be used on any platform that runs PowerShell. The source code is available on GitHub.

Installation and getting started

Install the module from the PowerShell Gallery:

Install-Module -Name PSScriptAnalyzer -Scope CurrentUser
# Or with PSResourceGet:
Install-PSResource -Name PSScriptAnalyzer

The main cmdlet is Invoke-ScriptAnalyzer. It can check a single file, an entire folder, or code passed as a string:

# Check a single file 
Invoke-ScriptAnalyzer -Path .\Deploy.ps1 
# Check a folder recursively 
Invoke-ScriptAnalyzer -Path .\src -Recurse 
# Check code directly 
Invoke-ScriptAnalyzer -ScriptDefinition 'gci | % { $_.Name }' 

The last example flags PSAvoidUsingCmdletAliases, among other findings, because gci and % are aliases. Each finding is a DiagnosticRecord object that you can filter, sort, or export as usual.

Rules and severity levels

Each rule has one of three severity levels. Use Get-ScriptAnalyzerRule to list all available rules, or see the rule reference for the full list.

Severity Meaning Example rule
Error Likely bug or security risk PSAvoidUsingConvertToSecureStringWithPlainText
Warning Best practice violation PSAvoidUsingCmdletAliases, PSUseApprovedVerbs
Information Suggestion about style or readability PSAvoidUsingPositionalParameters


You can filter directly when you run the cmdlet:

# Show only errors and warnings 
Invoke-ScriptAnalyzer -Path .\src -Recurse -Severity Error, Warning 
# Run only specific rules 
Invoke-ScriptAnalyzer -Path .\Deploy.ps1 -IncludeRule PSAvoidUsingWriteHost, PSUseDeclaredVarsMoreThanAssignments 

In addition to the built-in rules, the module supports custom rules that you write as PowerShell functions and load with -CustomRulePath.

Configuration and rule suppression

For projects, it’s worth using a settings file, typically PSScriptAnalyzerSettings.psd1 at the root of the repository. That way, everyone on the team runs the same checks:

@{ 
    Severity     = @('Error', 'Warning') 
    ExcludeRules = @('PSAvoidUsingWriteHost') 
    Rules        = @{ 
        PSUseCompatibleSyntax = @{ 
            Enable         = $true 
            TargetVersions = @('5.1', '7.4') 
        } 
    } 
} 

To use the file, run Invoke-ScriptAnalyzer -Path .\src -Recurse -Settings .\PSScriptAnalyzerSettings.psd1. The module also comes with predefined profiles. For example, -Settings PSGallery applies the same rules as the PowerShell Gallery.

If a finding is intentional in a particular case, you can suppress it directly in the code with the SuppressMessageAttribute attribute:

function Show-Banner { 
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Interaktive Ausgabe gewollt')] 
    param() 

    Write-Host 'Willkommen!' -ForegroundColor Cyan 
}

The explanation in the Justification field is optional, but it’s helpful during code review.

Automatic fixes, formatting, and integration

PSScriptAnalyzer can fix many of the issues it finds. With -Fix, for example, the module replaces aliases with full cmdlet names. Before running it, make sure the current version of your code is saved in version control:

Invoke-ScriptAnalyzer -Path .\Deploy.ps1 -Fix

For consistent indentation and brace placement, use Invoke-Formatter. It takes code as a string and returns the formatted code:

$code = Get-Content .\Deploy.ps1 -Raw 
Invoke-Formatter -ScriptDefinition $code | Set-Content .\Deploy.ps1 

Visual Studio Code: The PowerShell extension runs PSScriptAnalyzer in the background. Findings appear as underlines directly in the editor. Use the powershell.scriptAnalysis.settingsPath setting to point to your project’s settings file.

CI/CD: In your pipeline, you can fail the build when the analyzer reports errors, for example in GitHub Actions or Azure DevOps:

$results = Invoke-ScriptAnalyzer -Path .\src -Recurse -Settings .\PSScriptAnalyzerSettings.psd1 
$results | Format-Table -AutoSize 
if ($results | Where-Object Severity -eq 'Error') { 
    throw "PSScriptAnalyzer hat $($results.Count) Probleme gefunden." 
} 

Alternatively, wrap the check in a Pester test so the analysis results and unit test results appear in the same report.

PSScriptAnalyzer and ScriptRunner

ScriptRunner is a platform that lets teams manage PowerShell scripts centrally, delegate them to groups of users, and automate their execution. Because many users run scripts they didn’t write themselves, only code that has passed its checks should make it onto the platform.

A quality gate before deployment works well: keep your scripts in Git, have the pipeline check them against your settings file, and deploy them to ScriptRunner only if no errors are found, for example, through a connected Git repository. A few rules are especially valuable here:

  • PSAvoidUsingPlainTextForPassword and PSAvoidUsingConvertToSecureStringWithPlainText flag hard-coded credentials. Those belong in ScriptRunner’s central credential store, not in the script.
  • PSAvoidUsingWriteHost is useful because delegated scripts should return results through the output streams so they show up correctly in the results report.
  • PSUseDeclaredVarsMoreThanAssignments and PSReviewUnusedParameter help keep the param() block lean. That matters because ScriptRunner uses the parameters to generate the input form for the people running the script.
    ‍

This keeps self-service easy without compromising quality or security.

Conclusion

PSScriptAnalyzer takes just a few minutes to set up and keeps catching issues before they reach production. Start by running Invoke-ScriptAnalyzer on your existing code, then create a shared settings file and configure your pipeline to fail on errors. If you deploy scripts through ScriptRunner, make analysis a requirement for every release. That way, good PowerShell practices become the standard rather than a judgment call.

Sources