Skip to content

Get-AstFunctionName

SYNOPSIS

Retrieves the names of functions from an abstract syntax tree (Ast) in a PowerShell script.

SYNTAX

Path

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

Script

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

DESCRIPTION

Parses a PowerShell script file or script content to extract function names using an abstract syntax tree (Ast). The function supports searching by name, parsing from a file path, or directly from a script string. It can also search within nested functions and script block expressions when the -Recurse switch is used.

EXAMPLES

EXAMPLE 1

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

Output:

Get-Data
Set-Configuration

Extracts function names from the specified PowerShell script file.

EXAMPLE 2

Get-AstFunctionName -Script "function Test-Function { param($x) Write-Host $x }"

Output:

Test-Function

Extracts function names from the given script string.

EXAMPLE 3

Get-AstFunctionName -Path "C:\Scripts\example.ps1" -Recurse

Output:

Get-Data
Set-Configuration
Helper-Function

Extracts function names from the specified script file, including nested functions.

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

-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

System.String

NOTES

The name of each function found in the PowerShell script.

https://psmodule.io/Ast/Functions/Functions/Get-AstFunctionName/