Skip to content

Get-ScriptAST

SYNOPSIS

Parses a PowerShell script or script file and returns its Abstract Syntax Tree (AST).

SYNTAX

Path

Get-ScriptAST -Path <String> [-Tokens <PSReference>] [-Errors <PSReference>]
 [-ProgressAction <ActionPreference>] [<CommonParameters>]

Script

Get-ScriptAST -Script <String> [-Tokens <PSReference>] [-Errors <PSReference>]
 [-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

This function parses a PowerShell script from a file or a string input and returns its AST. It can optionally provide token and error information.

EXAMPLES

EXAMPLE 1

Get-ScriptAST -Path "C:\Scripts\MyScript.ps1"

Parses the PowerShell script at "MyScript.ps1" and returns its AST.

EXAMPLE 2

Get-ScriptAST -Script "Get-Process | Select-Object Name"

Parses the provided PowerShell script string and returns its AST.

EXAMPLE 3

Get-ScriptAST -Path "C:\Scripts\MyScript.ps1" -Tokens ([ref]$tokens) -Errors ([ref]$errors)

Parses the script file while also capturing tokens and 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

-Tokens

Reference variable to store parsed tokens.

Type: PSReference
Parameter Sets: (All)
Aliases:

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

-Errors

Reference variable to store parsing errors.

Type: PSReference
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
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

System.Management.Automation.Language.ScriptBlockAst

NOTES

https://psmodule.io/AST/Functions/Core/Get-ScriptAST/