Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (52 sloc) 2.03 KB
function GetScriptFunctions {
Outputs all functions within a Script or ScriptBlock.
Uses AST to retrieve all functions from a valid script file,
then outputs them all. Useful for dot-sourcing functions from
a file that also contains running code.
GetScriptFunctions -Export -ScriptPath .\testscript.ps1 | iex
(GetScriptFunctions -ScriptPath .\testscript.ps1).Count
Get-ChildItem -Recurse -Filter *.ps1 | GetScriptFunctions | Set-Content 'functions.psm1'
# FilePath for script to analyse
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)]
[ValidateScript({Test-Path $_})]
# To export the function(s) extent alone, or with full AST data
process {
foreach ($Item in Convert-Path $ScriptPath) {
try {
Write-Verbose "Analysing [$Item]"
$Tokens, $Errors = @()
$Script = [System.Management.Automation.Language.Parser]::ParseFile($Item, [ref]$Tokens, [ref]$Errors)
$Errors | ForEach-Object {
Write-Error -Message $_ -ErrorAction Stop
} catch {
Write-Error "Failed to parse File '$($Item)'.`n$_"
try {
$functions = $Script.FindAll( {$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
Write-Verbose "Found $($functions.count) functions."
} catch {
Write-Error "Failed to find any functions in File '$($Item)'.`n$_"
if ($Export) {
} else {
You can’t perform that action at this time.