pf-dependencies.ps1

function Get-Module_Dependent_Commands {
    param (
        [Parameter(ValueFromPipeline=$true)]
        $path,
        [string[]]$Module
    )
    Process {
        $path = Get-Path $path
        $dependencies = Get-ScriptCommandDependencies -file $path
            | Where-Object { $_.Source -in $Module }
        return $dependencies
    }
}
function Get-Module_Dependent:::Example {
    Get-ChildItem -Filter *.ps1 -Recurse 
        | Get-Module_Dependent_Commands -Module "pf-smb"
}

function Get-ScriptCommandDependencies {
    # based on https://mikefrobbins.com/2019/02/21/powershell-tokenizer-more-accurate-than-ast-in-certain-scenarios/
    # see also https://mikefrobbins.com/2019/05/17/using-the-ast-to-find-module-dependencies-in-powershell-functions-and-scripts/
    param (
        [Parameter(ValueFromPipeline=$true)]
        $file,
        [Switch]$FailOnMissingCommand,
        [Switch]$IgnoreSameModule
    )
    begin {
        $getCommandErrorAction =  ($FailOnMissingCommand) ? 'Stop' : 'SilentlyContinue' 
    }
    process {
        $file = $file | Get-Path
        $Token = $null
        $ignoredOutput = [System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$Token, [ref]$null)
        Write-Verbose $ignoredOutput
        $commandTokenList = $Token | Where-Object {$_.TokenFlags -eq 'CommandName'}
        if (-not $commandTokenList) { return }
        $commandNameList = $commandTokenList.Text.ToLowerInvariant() 
            | Select-Object -Unique | Sort-Object
        $commandList = $commandNameList | ForEach-Object {
            Get-Command -name $_ -ErrorAction $getCommandErrorAction
        }
        $commandList = $commandList | Where-Object { $_.Module.Path }

        if ($IgnoreSameModule) {
            $commandList = $commandList | Where-Object { 
                $cmdPath = $_.Module.Path
                $moduleFolder = split-path -Path  $cmdPath -Parent 
                -not (Test-Path_Parent -path $file -parent $moduleFolder)
            }
        }
        $commandList
    }
}
function Get-ScriptCommandDependencies:::Examples{
    $result = Get-ChildItem -Path .\pf-AzPipelines -filter *.ps1 -Recurse
        | Get-ScriptCommandDependencies -IgnoreSameModule
    $result
}

function Get-ModuleDependencies {
    param (
        [Parameter(ValueFromPipeline=$true)]
        $file,
        [Switch]$FailOnMissingCommand 
    )
    process {
        $commandList = $file |  Get-ScriptCommandDependencies -IgnoreSameModule `
            -FailOnMissingCommand:($FailOnMissingCommand.IsPresent)
        $commandList.Module
    }
}
function Get-ModuleDependencies:::Example {
    $result = Get-ChildItem -Path .\pf-AzPipelines -filter *.ps1 -Recurse | 
        Get-ModuleDependencies
    $result
}