Inventory syntax errors, functions, command sites and method calls without executing the target.
How to use it
.\Review-SocPowerShellAst.ps1 -LiteralPath 'C:\Lab\example.ps1'
Platform and permissions
Read access to a local script. Parser follows the running PowerShell version.
What changes / what is read
Reads/parses source only; never dot-sources, imports or executes the reviewed script.
Scope and interpretation
Verb labels are hints. Aliases, shadowing, generated code, dependencies, reachability and intent remain unresolved. Malformed input limits parser recovery; static analysis cannot certify safety.
Practice task
Review a harmless script with Get, Set and Remove command text; explain why command names alone cannot establish intent.
Reference
Microsoft documentation. Review the documentation for your installed version and use only an authorised learning or investigation environment.
This is personal educational material. Review the complete code and your signing policy before use. PowerShell code is displayed and downloaded as text; WordPress does not run it. Collection caps limit rows/files, not every provider’s wall-clock execution time.
#requires -Version 5.1
[CmdletBinding()]
param([Parameter(Mandatory)][string]$LiteralPath,[ValidateRange(1,20)][int]$MaxMB=5)
$file=Get-Item -LiteralPath $LiteralPath -ErrorAction Stop
if($file.PSIsContainer -or $file.Length -gt ($MaxMB*1MB)){throw 'Choose a regular script file within the size cap.'}
if($file.Attributes -band [IO.FileAttributes]::ReparsePoint){throw 'Reparse-point input is not accepted.'}
$tokens=$null;$parseErrors=$null
# Parsing reads source text. It does NOT execute, dot-source or import the target.
$ast=[System.Management.Automation.Language.Parser]::ParseFile($file.FullName,[ref]$tokens,[ref]$parseErrors)
$commands=@($ast.FindAll({param($n) $n -is [System.Management.Automation.Language.CommandAst]},$true) | ForEach-Object {
$name=$_.GetCommandName();$group='Other / unresolved'
if($name -match '^(Get|Find|Test|Select|Where|Measure)-'){$group='Read / inspect candidate'}
elseif($name -match '^(Set|New|Add|Enable|Disable|Start|Stop|Restart)-'){$group='Change / lifecycle candidate'}
elseif($name -match '^(Remove|Clear|Uninstall)-'){$group='Remove / clear candidate'}
[pscustomobject]@{Line=$_.Extent.StartLineNumber;Name=$name;Category=$group;Dynamic=($null -eq $name);Invocation=[string]$_.InvocationOperator;Source=$_.Extent.Text}
})
$functions=@($ast.FindAll({param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst]},$true) | ForEach-Object {[pscustomobject]@{Name=$_.Name;Line=$_.Extent.StartLineNumber}})
$methods=@($ast.FindAll({param($n) $n -is [System.Management.Automation.Language.InvokeMemberExpressionAst]},$true) | ForEach-Object {[pscustomobject]@{Line=$_.Extent.StartLineNumber;Source=$_.Extent.Text}})
[pscustomobject]@{Path=$file.FullName;ParserVersion=$PSVersionTable.PSVersion.ToString();ParseErrors=@($parseErrors | ForEach-Object {[pscustomobject]@{Line=$_.Extent.StartLineNumber;Message=$_.Message}});Commands=$commands;Functions=$functions;MethodCalls=$methods;Note='Verb-based categories are review hints, not safety findings. Aliases, shadowing, dynamic code, external modules, reachability and intent are not resolved. All parsed function bodies are inventoried, including uncalled code; parser recovery after malformed input can be incomplete.'}
Example code is displayed as text. Review it and test in your own lab before use.