Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active October 18, 2018 11:02
Show Gist options
  • Save markwragg/aab1f5b1fe7a38a80c419eaee7d428b5 to your computer and use it in GitHub Desktop.
Save markwragg/aab1f5b1fe7a38a80c419eaee7d428b5 to your computer and use it in GitHub Desktop.
Find all cmdlets being used in a specified script by using Abstract Syntax Trees
$AST = [System.Management.Automation.Language.Parser]::ParseFile(
(Resolve-Path './Some-File.ps1'),
[ref]$null,
[ref]$Null
)
$CommandElements = ($AST.FindAll( {$args[0] -is [System.Management.Automation.Language.CommandAst]}, $true)).CommandElements
($CommandElements | Where-Object {$_.StringConstantType -eq 'BareWord'}).Value | Sort-Object -Unique
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment