Skip to content

Get-AstScript

SYNOPSIS

Parses a PowerShell script or script file and returns its abstract syntax tree (Ast).

SYNTAX

Path

Get-AstScript -Path <String> [-ProgressAction <ActionPreference>] [<CommonParameters>]

Script

Get-AstScript -Script <String> [-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

The Get-AstScript function parses a PowerShell script or script file and returns its abstract syntax tree (Ast), along with tokens and errors encountered during parsing. This function can be used to analyze the structure of a script by specifying either the script content directly or the path to a script file.

EXAMPLES

EXAMPLE 1

Get-AstScript -Path "C:\Scripts\example.ps1"

Output:

Ast    : [System.Management.Automation.Language.ScriptBlockAst]
Tokens : {Token1, Token2, ...}
Errors : {Error1, Error2, ...}

Parses the PowerShell script located at "C:\Scripts\example.ps1" and returns its Ast, tokens, and any parsing errors.

EXAMPLE 2

Get-AstScript -Script "Write-Host 'Hello World'"

Output:

Ast    : [System.Management.Automation.Language.ScriptBlockAst]
Tokens : {Token1, Token2, ...}
Errors : {}

Parses the provided PowerShell script string and returns its Ast, tokens, and any parsing errors.

PARAMETERS

-Path

The path to the PowerShell script file to be parsed. Validate using Test-Path

Type: String
Parameter Sets: Path
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-Script

The PowerShell script to be parsed.

Type: String
Parameter Sets: Script
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

PSCustomObject

NOTES

The returned custom object contains the following properties: - Ast - [System.Management.Automation.Language.ScriptBlockAst]. The abstract syntax tree (Ast) of the parsed script. - Tokens - [System.Management.Automation.Language.Token[]]. The tokens generated during parsing. - Errors - [System.Management.Automation.Language.ParseError[]]. Any parsing errors encountered.

https://psmodule.io/Ast/Functions/Core/Get-AstScript/