{"id":8724,"date":"2021-12-15T09:43:08","date_gmt":"2021-12-15T14:43:08","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8724"},"modified":"2021-12-15T10:18:13","modified_gmt":"2021-12-15T15:18:13","slug":"discovering-aliases-with-the-powershell-ast","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","title":{"rendered":"Discovering Aliases with the PowerShell AST"},"content":{"rendered":"\n<p>I've been working on a new PowerShell module that incorporates code from a few of my recent posts on converting PowerShell scripts and functions to files. I even whipped up a script, think of it as a meta-script, to create the module using the commands that I am adding to the module. I'll cover that another day. In the process of writing this script, I realized I also wanted to identify aliases for functions defined in the files.<\/p>\n\n\n\n<p>Specifically, I wanted to find aliases defined in the function itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function Get-Foo {\n  [cmdletbinding()]\n  [alias(\"gf\")]\n  Param (...)\n...<\/code><\/pre>\n\n\n\n<p>I could have simply dot sourced the script file and then looked for the aliases. But I didn't want to assume that dot sourcing was an option. Instead, I went back to parsing the file with the AST.<\/p>\n\n\n\n<p>Unfortunately, I couldn't find anything in the AST that specifically retrieved command aliases. If I'm missing something, please let me know. Instead, I relied on regular expressions to parse the function body. Using the code I've shown in the previous posts, it is simple by now to find functions in files. <\/p>\n\n\n\n<p> <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1013\" height=\"346\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png\" alt=\"match [alias()] regex\" class=\"wp-image-8725\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png 1013w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string-300x102.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string-768x262.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string-850x290.png 850w\" sizes=\"auto, (max-width: 1013px) 100vw, 1013px\" \/><\/a><\/figure>\n\n\n\n<p>This is merely a proof-of-concept. I still need to extract the alias names. I'll use a regular expression pattern that uses lookahead and lookbehind.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">[regex]$rx = \"(?&lt;=alias\\().*(?=\\)\\])\"<\/code><\/pre>\n\n\n\n<p>The expression says to match on anything using .* where the text that comes before (lookbehind) is \"alias(\" and where the text after (lookahead) is \")]\". Parentheses and square brackets are regex characters so I need to escape them with a slash. I can then clean up the match and get the aliases.<\/p>\n\n\n\n<p>Here's my PowerShell function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function Get-FunctionAlias {\n    [cmdletbinding()]\n    [alias(\"gfal\", \"ga\")]\n    [outputType(\"string\")]\n    Param(\n        [Parameter(Position = 0, Mandatory, HelpMessage = \"Specify the .ps1 or .psm1 file with defined functions.\")]\n        [ValidateScript( {\n            If (Test-Path $_ ) {\n                $True\n            }\n            Else {\n                Throw \"Can't validate that $_ exists. Please verify and try again.\"\n                $False\n            }\n        })]\n        [ValidateScript( {\n            If ($_ -match \"\\.ps(m)?1$\") {\n                $True\n            }\n            Else {\n                Throw \"The path must be to a .ps1 or .psm1 file.\"\n                $False\n            }\n        })]\n        [string]$Path\n    )\n\n    New-Variable astTokens -Force\n    New-Variable astErr -Force\n    $Path = Convert-Path -Path $path\n    [regex]$rx = \"(?&lt;=alias\\().*(?=\\)\\])\"\n    Write-Verbose \"Parsing $path for functions.\"\n    $AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$astTokens, [ref]$astErr)\n\n    #parse out functions using the AST\n    $functions = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)\n    if ($functions.count -gt 0) {\n        foreach ($f in $functions) {\n            if ($rx.IsMatch($f.body)) {\n                [pscustomobject]@{\n                    Name  = $f.name\n                    #remove quotes from the alias names and join as a comma-separated array\n                    Alias = ($rx.matches($f.body).value -replace \"\"\"|'\", \"\") -split \",\"\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Let's use the function to discover its own aliases.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"276\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias-1024x276.png\" alt=\"Get-FunctionAlias example\" class=\"wp-image-8727\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias-1024x276.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias-300x81.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias-768x207.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias-850x229.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/get-functionalias.png 1050w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>My PowerShell script file has two functions with defined aliases. I can use this information when scripting the build of a new PowerShell module. That topic is next on the agenda.<\/p>\n\n\n\n<p>In the meantime, I hope you'll give this a spin and let me know how you might use it or what would make it even more helpful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Update<\/h2>\n\n\n\n<p>Shortly after posting this, <a href=\"https:\/\/twitter.com\/PrzemyslawKlys\" target=\"_blank\" rel=\"noreferrer noopener\">Przemys\u0142aw K\u0142ys<\/a> was kind enough to share some code that uses the PowerShell AST to extract alias information. His approach is much more elegant, although I'm always looking for a way to use regular expressions. Here is a revised version of the function. The output is unchanged.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function Get-FunctionAlias {\n    [cmdletbinding()]\n    [alias(\"gfal\", \"ga\")]\n    [outputType(\"string\")]\n    Param(\n        [Parameter(Position = 0, Mandatory, HelpMessage = \"Specify the .ps1 or .psm1 file with defined functions.\")]\n        [ValidateScript( {\n            If (Test-Path $_ ) {\n                $True\n            }\n            Else {\n                Throw \"Can't validate that $_ exists. Please verify and try again.\"\n                $False\n            }\n        })]\n        [ValidateScript( {\n            If ($_ -match \"\\.ps(m)?1$\") {\n                $True\n            }\n            Else {\n                Throw \"The path must be to a .ps1 or .psm1 file.\"\n                $False\n            }\n        })]\n        [string]$Path\n    )\n\n    New-Variable astTokens -Force\n    New-Variable astErr -Force\n    $Path = Convert-Path -Path $path\n    Write-Verbose \"Parsing $path for functions.\"\n    $AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$astTokens, [ref]$astErr)\n\n    #parse out functions using the AST\n    $functions = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)\n    if ($functions.count -gt 0) {\n        foreach ($f in $functions) {\n            #thanks to https:\/\/twitter.com\/PrzemyslawKlys for this suggestion\n            $aliasAST =  $f.FindAll( {\n                $args[0] -is [System.Management.Automation.Language.AttributeAst] -and\n                $args[0].TypeName.Name -eq 'Alias' -and\n                $args[0].Parent -is [System.Management.Automation.Language.ParamBlockAst]\n            }, $true)\n\n            if ($aliasAST.positionalArguments) {\n                [pscustomobject]@{\n                    Name  = $f.name\n                    Alias = $aliasAST.PositionalArguments.Value\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a new PowerShell module that incorporates code from a few of my recent posts on converting PowerShell scripts and functions to files. I even whipped up a script, think of it as a meta-script, to create the module using the commands that I am adding to the module. I&#8217;ll cover that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Finding Aliases with the PowerShell AST.","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[447,534,502,540],"class_list":["post-8724","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-ast","tag-powershell","tag-regular-expression","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Discovering Aliases with the PowerShell AST &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"Continuing my exploration of the PowerShell AST, here&#039;s how I can discover defined aliases for a function in a PowerShell script file.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Discovering Aliases with the PowerShell AST &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Continuing my exploration of the PowerShell AST, here&#039;s how I can discover defined aliases for a function in a PowerShell script file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-15T14:43:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-15T15:18:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Discovering Aliases with the PowerShell AST\",\"datePublished\":\"2021-12-15T14:43:08+00:00\",\"dateModified\":\"2021-12-15T15:18:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/\"},\"wordCount\":395,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/find-alias-string.png\",\"keywords\":[\"AST\",\"PowerShell\",\"Regular Expression\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/\",\"name\":\"Discovering Aliases with the PowerShell AST &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/find-alias-string.png\",\"datePublished\":\"2021-12-15T14:43:08+00:00\",\"dateModified\":\"2021-12-15T15:18:13+00:00\",\"description\":\"Continuing my exploration of the PowerShell AST, here's how I can discover defined aliases for a function in a PowerShell script file.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/find-alias-string.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/find-alias-string.png\",\"width\":1013,\"height\":346},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8724\\\/discovering-aliases-with-the-powershell-ast\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Discovering Aliases with the PowerShell AST\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Discovering Aliases with the PowerShell AST &#8226; The Lonely Administrator","description":"Continuing my exploration of the PowerShell AST, here's how I can discover defined aliases for a function in a PowerShell script file.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","og_locale":"en_US","og_type":"article","og_title":"Discovering Aliases with the PowerShell AST &#8226; The Lonely Administrator","og_description":"Continuing my exploration of the PowerShell AST, here's how I can discover defined aliases for a function in a PowerShell script file.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-12-15T14:43:08+00:00","article_modified_time":"2021-12-15T15:18:13+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Discovering Aliases with the PowerShell AST","datePublished":"2021-12-15T14:43:08+00:00","dateModified":"2021-12-15T15:18:13+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/"},"wordCount":395,"commentCount":2,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png","keywords":["AST","PowerShell","Regular Expression","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","name":"Discovering Aliases with the PowerShell AST &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png","datePublished":"2021-12-15T14:43:08+00:00","dateModified":"2021-12-15T15:18:13+00:00","description":"Continuing my exploration of the PowerShell AST, here's how I can discover defined aliases for a function in a PowerShell script file.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png","width":1013,"height":346},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Discovering Aliases with the PowerShell AST"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":8741,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8741\/building-a-powershell-module-inception-style\/","url_meta":{"origin":8724,"position":0},"title":"Building a PowerShell Module Inception-Style","author":"Jeffery Hicks","date":"December 17, 2021","format":false,"excerpt":"Over the course of the last week or so, I've been sharing PowerShell functions and scripts for working with PowerShell functions and scripts. I showed PowerShell functions to export functions to a script file and code to convert scripts to functions It has all been very Inception-like. To wrap this\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/psinception.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3613,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3613\/updated-powershell-script-profiler\/","url_meta":{"origin":8724,"position":1},"title":"Updated PowerShell  Script Profiler","author":"Jeffery Hicks","date":"January 15, 2014","format":false,"excerpt":"Last year I wrote a sidebar for the Scripting Guy, Ed Wilson and an update to his PowerShell Best Practices book. I wrote a script using the new parser in PowerShell 3.0 to that would analyze a script and prepare a report showing what commands it would run, necessary parameters,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"script-profile-report","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/01\/script-profile-report.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/01\/script-profile-report.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/01\/script-profile-report.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1729,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1729\/ise-scripting-geek-module\/","url_meta":{"origin":8724,"position":2},"title":"ISE Scripting Geek Module","author":"Jeffery Hicks","date":"October 27, 2011","format":false,"excerpt":"Over the last year I've posted functions I've written to extend the Windows PowerShell ISE. I have finally pulled everything together into a module I'm calling the ISE Scripting Geek. Download and extract the zip file (below) to your modules folder. You should end up with a path like 'C:\\Users\\Jeff\\Documents\\WindowsPowerShell\\Modules\\ISEScriptingGeek'.\u2026","rel":"","context":"In &quot;PowerShell ISE&quot;","block_context":{"text":"PowerShell ISE","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-ise\/"},"img":{"alt_text":"ISE Scripting Geek add-on menu","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/isescriptinggeek-300x200.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":8709,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8709\/converting-powershell-scripts-to-functions\/","url_meta":{"origin":8724,"position":3},"title":"Converting PowerShell Scripts to Functions","author":"Jeffery Hicks","date":"December 10, 2021","format":false,"excerpt":"Recently, I shared some PowerShell code to export a function to a file. It was a popular post. My friend Richard Hicks (no relation) thought we was joking when he asked about converting files to functions. His thought was to take a bunch of PowerShell scripts, turn them into a\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/poc-modulecommands.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/poc-modulecommands.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/poc-modulecommands.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/poc-modulecommands.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":8793,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-7\/8793\/profile-powershell-functions\/","url_meta":{"origin":8724,"position":4},"title":"Profile PowerShell Functions","author":"Jeffery Hicks","date":"January 17, 2022","format":false,"excerpt":"I've published a stable release of the PSFunctionTools module to the PowerShell Gallery. Previously, it was pre-release. The module requires PowerShell 7.1 and later. Although, as I have mentioned in the past, you are welcome to fork the repository and create a version that will run on Windows PowerShell. I\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/01\/get-functionprofile2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/01\/get-functionprofile2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/01\/get-functionprofile2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/01\/get-functionprofile2.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1330,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-v2-0\/1330\/powershell-ise-convert-all-aliases\/","url_meta":{"origin":8724,"position":5},"title":"PowerShell ISE Convert All Aliases","author":"Jeffery Hicks","date":"April 8, 2011","format":false,"excerpt":"Yesterday I posted an article on how to convert a selected word to an alias or cmdlet. While I think there is still some value in this piecemeal approach. sometimes you want to make wholesale changes, such as when troubleshooting a script that someone else wrote that is full of\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8724"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8724\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}