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 SilentlyContinueMicrosoft 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 CurrentUserIf Install-PSResource was not listed, use PowerShellGet instead:
Get-PSRepository -Name PSGallery
Install-Module -Name PSScriptAnalyzer -Repository PSGallery -Scope CurrentUserCurrentUser 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 PSScriptAnalyzerYou 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 PSAvoidUsingCmdletAliasesThen check the full command name:
Invoke-ScriptAnalyzer -ScriptDefinition 'Get-ChildItem .' -IncludeRule PSAvoidUsingCmdletAliasesNo 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 -WrapRead 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 utf8This 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 PSGallerySettings 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 $textCompare 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' -FixWhatIf 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
- Command not recognised: check the installed module and import it in the same PowerShell edition you are using.
- Repository or download failure: verify the named repository and your permitted network/proxy route. Do not disable certificate checking or security protection.
- File not found: check the full path, extension and quotes. Use
Test-Path -LiteralPath 'C:\MyScripts\MyScript.ps1'. - No findings: confirm you checked the intended file and inspect active settings, filters and suppressions.
- A fix changed too much: return to your untouched original or version-control copy; review the particular rule before trying again.
