Skip to content

Instantly share code, notes, and snippets.

@adamdriscoll
Last active July 1, 2016 02:21
Show Gist options
  • Save adamdriscoll/96e46b8be439d4fd2a12c4f80ac59cd7 to your computer and use it in GitHub Desktop.
Save adamdriscoll/96e46b8be439d4fd2a12c4f80ac59cd7 to your computer and use it in GitHub Desktop.
Finds aliases set by Set-Alias
$Ast = {
Set-Alias -Name Test -Value SomeValue
New-Alias -Name SomeOtherAlias -Value SomeValue
}.Ast
$SetAliasCommands = $Ast.FindAll({$args[0] -is [System.Management.Automation.Language.CommandAst] -and ($Command.CommandElements[0].Value -eq 'Set-Alias' -or $Command.CommandElements[0].Value -eq 'New-Alias')}, $true)
foreach($SetAliasCommand in $SetAliasCommands)
{
$NextElementIsNameValue = $false
foreach($element in $SetAliasCommand.CommandElements)
{
if ($NextElementIsNameValue)
{
$element.Value
break
}
if ($element -is [System.Management.Automation.Language.CommandParameterAst] -and $element.ParameterName -eq 'Name')
{
$NextElementIsNameValue = $true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment