Skip to content

Back to the PowerShell desk

PSScriptAnalyzer: check a script before you run it

Free module · local analysis · instructions checked against Microsoft documentation on 27 November 2026.

1. Check your PowerShell version

Open PowerShell from your applications menu. Use a normal user session. Run:

$PSVersionTable.PSVersion
Get-Command Install-PSResource, Install-Module -ErrorAction SilentlyContinue

Microsoft lists Windows PowerShell 5.1 or later, or PowerShell 7.2.11 or later on Windows, Linux and macOS. Use a currently supported PowerShell release. The second command shows which installation tools are available; use one route below, not both.

2. Download and install the module

Installation downloads software from PowerShell Gallery. On a managed computer, follow your organisation’s software rules. Check repository details and any trust or dependency prompt before proceeding.

With PSResourceGet

Get-PSResourceRepository -Name PSGallery
Install-PSResource -Name PSScriptAnalyzer -Repository PSGallery -Scope CurrentUser

If Install-PSResource was not listed, use PowerShellGet instead:

Get-PSRepository -Name PSGallery
Install-Module -Name PSScriptAnalyzer -Repository PSGallery -Scope CurrentUser

CurrentUser installs for your account. Do not change the repository to trusted just to remove a prompt. These commands deliberately omit automatic trust, forced replacement and licence acceptance switches. If installation fails, read the specific error rather than repeatedly forcing it.

Confirm it is available

Get-Module -ListAvailable PSScriptAnalyzer | Select-Object Name, Version, Path
Import-Module PSScriptAnalyzer
Get-Command -Module PSScriptAnalyzer

You should see the module and commands including Invoke-ScriptAnalyzer and Invoke-Formatter. After an update, open a fresh PowerShell session and import the module again.

3. Try a harmless text example

This checks a quoted example; it does not run the example command. The alias rule should identify gci as shorthand. Exact wording and severity can vary by installed version.

Invoke-ScriptAnalyzer -ScriptDefinition 'gci .' -IncludeRule PSAvoidUsingCmdletAliases

Then check the full command name:

Invoke-ScriptAnalyzer -ScriptDefinition 'Get-ChildItem .' -IncludeRule PSAvoidUsingCmdletAliases

No output here means that this selected rule reported no finding. It says nothing about rules you did not run.

4. Check your downloaded script

Save the desk’s download and open it in a text editor first. Replace the example path below with that file’s full path, keeping the quotes. The example uses Windows paths; use your actual path on macOS or Linux.

Invoke-ScriptAnalyzer -Path 'C:\MyScripts\MyScript.ps1' |
    Select-Object RuleName, Severity, ScriptName, Line, Message |
    Format-Table -Wrap

Read the line number and message, make one understood change, then repeat the check. Start with syntax problems (ParseError), then investigate Error and Warning findings. Information findings can highlight style or maintainability. A clean report does not prove correct behaviour, safe permissions or suitability for your data.

Check a folder or save a report
$findings = Invoke-ScriptAnalyzer -Path 'C:\MyScripts' -Recurse
$findings | Select-Object RuleName, Severity, ScriptName, Line, Message |
    Export-Csv -LiteralPath '.\analysis-report.csv' -NoTypeInformation -Encoding utf8

This writes analysis-report.csv in the current folder and replaces an existing file with that name. Choose a new filename if needed. Reports may contain script names and details: review them before sharing.

5. Understand rules and settings

Get-ScriptAnalyzerRule | Select-Object RuleName, Severity
Invoke-ScriptAnalyzer -Path 'C:\MyScripts\MyScript.ps1' -Settings PSGallery

Settings can be a preset, a hashtable or a trusted .psd1 file. Project settings can also be discovered automatically. Check which rules and severity filters are active when comparing results. Do not hide a finding merely to make the report empty. Custom rules are executable code: load only rules you trust. Compatibility rules need the right target configuration; ordinary analysis is not a complete compatibility test.

6. Format text and handle automatic fixes carefully

Formatting returns revised text; this example displays it without replacing your file:

$text = Get-Content -LiteralPath 'C:\MyScripts\MyScript.ps1' -Raw
Invoke-Formatter -ScriptDefinition $text

Compare the returned text in your editor before saving. For automatic fixes, use a separate copy or version control first. -Fix can modify files and only handles rules with a supported correction.

# Work only on your disposable review copy
Invoke-ScriptAnalyzer -Path 'C:\MyScripts\MyScript-review.ps1' -Fix -WhatIf
# After reviewing the proposed operation, apply to that copy
Invoke-ScriptAnalyzer -Path 'C:\MyScripts\MyScript-review.ps1' -Fix

WhatIf is a preview of the operation, not a complete line-by-line diff. Compare the changed copy with your original, rerun analysis, then test behaviour using authorised sample data. Keep the original until the result is verified.

If something goes wrong

Official instructions and references

Use the script preparation checklist · Return to the editor