Skip to content

Get‑ClassAst

viscalyxbot edited this page Sep 16, 2025 · 1 revision

SYNOPSIS

Gets class definitions from a PowerShell script file using AST parsing.

SYNTAX

String (Default)

Get-ClassAst -Path <String[]> [-ClassName <String>] [<CommonParameters>]

FileInfo

Get-ClassAst -ScriptFile <FileInfo[]> [-ClassName <String>] 
 [<CommonParameters>]

DESCRIPTION

The Get-ClassAst function parses a PowerShell script file and extracts class definitions using Abstract Syntax Tree (AST) parsing. It can return all classes in the file or filter for a specific class by name.

EXAMPLES

EXAMPLE 1

Get-ClassAst -Path 'C:\Scripts\MyClasses.ps1'

Returns all class definitions found in the specified script file.

EXAMPLE 2

Get-ClassAst -Path 'C:\Scripts\MyClasses.ps1' -ClassName 'MyDscResource'

Returns only the 'MyDscResource' class definition from the specified script file.

EXAMPLE 3

'C:\Scripts\Class1.ps1', 'C:\Scripts\Class2.ps1' | Get-ClassAst

Returns all class definitions found in the specified script files using pipeline input.

EXAMPLE 4

Get-ChildItem -Path 'C:\Scripts\*.ps1' | Get-ClassAst -ClassName 'MyDscResource'

Returns 'MyDscResource' class definitions from all PowerShell script files in the specified directory.

PARAMETERS

-ClassName

Optional parameter to filter for a specific class by name. If not provided, all classes in the script file are returned.

Type: String
Parameter Sets: (All)
Aliases:

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

-Path

The path(s) to the PowerShell script file(s) to parse. Accepts pipeline input from strings (file paths).

Type: String[]
Parameter Sets: String
Aliases:

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

-ScriptFile

FileInfo object(s) representing the PowerShell script file(s) to parse. Accepts pipeline input from Get-ChildItem and other commands that return FileInfo objects.

Type: FileInfo[]
Parameter Sets: FileInfo
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
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

System.String[]

You can pipe file paths as strings to this function.

System.IO.FileInfo[]

You can pipe FileInfo objects to this function.

OUTPUTS

System.Collections.Generic.IEnumerable`1System.Management.Automation.Language.Ast

Returns a collection of AST nodes (items are TypeDefinitionAst) representing

the class definitions found in the script file. Returns an empty collection

if no classes are found or when filtering for a non-existent class name.

NOTES

RELATED LINKS

Clone this wiki locally