Skip to content

Get-AstCommand

SYNOPSIS

Retrieves command Ast (Abstract Syntax Tree) elements from a PowerShell script or Ast object.

SYNTAX

Ast (Default)

Get-AstCommand [-Name <String>] -Ast <Ast> [-Recurse] [-ProgressAction <ActionPreference>] [<CommonParameters>]

Path

Get-AstCommand [-Name <String>] -Path <String> [-Recurse] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

Script

Get-AstCommand [-Name <String>] -Script <String> [-Recurse] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

DESCRIPTION

This function extracts and returns command Ast elements from a specified PowerShell script file, script content, or an existing Ast object. The function supports multiple input methods, including direct script text, file paths, or existing Ast objects. It also provides an option to search nested functions and script block expressions.

EXAMPLES

EXAMPLE 1

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

Output:

Ast    : {@{Name=Get-Process; Extent=...}, @{Name=Write-Host; Extent=...}}
Tokens : {...}
Errors : {}

Parses the specified script file and extracts command Ast elements.

EXAMPLE 2

Get-AstCommand -Script "Get-Process; Write-Host 'Hello'"

Output:

Ast    : {@{Name=Get-Process; Extent=...}, @{Name=Write-Host; Extent=...}}
Tokens : {...}
Errors : {}

Parses the provided script content and extracts command Ast elements.

EXAMPLE 3

$ast = [System.Management.Automation.Language.Parser]::ParseInput("Get-Process", [ref]$null, [ref]$null)
Get-AstCommand -Ast $ast

Output:

Ast    : {@{Name=Get-Process; Extent=...}}
Tokens : {...}
Errors : {}

Extracts command Ast elements from a manually parsed Ast object.

PARAMETERS

-Name

The name of the command to search for. Defaults to all commands ('*').

Type: String
Parameter Sets: (All)
Aliases:

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

-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

-Ast

An existing Ast object to search.

Type: Ast
Parameter Sets: Ast
Aliases:

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

-Recurse

Search nested functions and script block expressions.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
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

PSCustomObject

NOTES

Returns an object containing extracted Ast elements, tokens, and errors.

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