{"id":8693,"date":"2021-12-03T10:14:36","date_gmt":"2021-12-03T15:14:36","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8693"},"modified":"2021-12-03T10:14:39","modified_gmt":"2021-12-03T15:14:39","slug":"exporting-powershell-functions-to-files","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/","title":{"rendered":"Exporting PowerShell Functions to Files"},"content":{"rendered":"\n<p>When I write a PowerShell module, it typically includes more than one export function. Where you store your module functions is a great discussion topic and I don't think there is necessarily one best practice for everyone. I think it might depend on the number and complexity of the functions. Are other people contributing code to the module? How are you using Pester? These are just a few questions to consider. In my work, sometimes the functions are in the .psm1 file. Some projects have a public.ps1 file with several functions. In more complex projects, like my <a href=\"https:\/\/github.com\/jdhitsolutions\/PSScriptTools\" target=\"_blank\" rel=\"noreferrer noopener\">PSScriptTools <\/a>module, each function gets a separate file. <\/p>\n\n\n\n<p>My challenge arises when I decide to separate out all the functions defined in a single file. I want each function to reside in its own file, using the function name as the file name. I got tired of doing this manually, so I wrote a PowerShell function to do the work for me.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the AST<\/h2>\n\n\n\n<p>PowerShell uses a coding concept called the \"abstract syntax tree\", or AST. The AST makes it possible to parse a PowerShell expression or even a file. Using the AST is definitely advanced PowerShell scripting juju.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">New-Variable astTokens -Force\nNew-Variable astErr -Force\n$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$astTokens, [ref]$astErr)<\/code><\/pre>\n\n\n\n<p>Depending on what you are abstracting, you may be able to get exactly what you want from the immediate parsing. In my case, I needed to search within the AST result for a specific type of command element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png\"><img loading=\"lazy\" decoding=\"async\" width=\"839\" height=\"498\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png\" alt=\"\" class=\"wp-image-8694\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png 839w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member-300x178.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member-768x456.png 768w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/a><\/figure>\n\n\n\n<p>In my example, I found these functions in the file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-names.png\"><img loading=\"lazy\" decoding=\"async\" width=\"487\" height=\"272\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-names.png\" alt=\"\" class=\"wp-image-8695\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-names.png 487w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-names-300x168.png 300w\" sizes=\"auto, (max-width: 487px) 100vw, 487px\" \/><\/a><\/figure>\n\n\n\n<p>I'll point out that the function names that don't follow the Verb-Noun naming convention are class constructors. Although they could also be private helper functions with typically use non-standard names since they are never exposed publically. More on how I handle this in a bit.<\/p>\n\n\n\n<p>Each function element is its own object.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"357\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent-1024x357.png\" alt=\"\" class=\"wp-image-8696\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent-1024x357.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent-300x105.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent-768x268.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent-850x296.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/ast-extent.png 1194w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>As you can see, it is pretty easy to get the function text.  It is even easier if I use the ToString() method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\"> Set-Content -path \"d:\\temp\\$($functions[-1].name).ps1\" -Value $functions[-1].ToString()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Function Names<\/h2>\n\n\n\n<p>As I was building my export tool, I realized I needed a way to skip functions that probably will never be publicly exposed. So I wrote a simple function to test a function name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function Test-FunctionName {\n    [CmdletBinding()]\n    [OutputType(\"boolean\")]\n    Param(\n    [Parameter(Position = 0,Mandatory,HelpMessage = \"Specify a function name.\")]\n    [ValidateNotNullOrEmpty()]\n    [string]$Name\n    )\n\n    Write-Verbose \"Validating function name $Name\"\n    #Function name must first follow Verb-Noun pattern\n    if ($Name -match \"^\\w+-\\w+$\") {\n        #validate the standard verb\n        $verb = ($Name -split \"-\")[0]\n        Write-Verbose \"Validating detected verb $verb\"\n        if ((Get-Verb).verb -contains $verb ) {\n            $True\n        }\n        else {\n            Write-Verbose \"$($Verb.ToUpper()) is not an approved verb.\"\n            $False\n        }\n    }\n    else {\n        Write-Verbose \"$Name does not match the regex pattern ^\\w+-\\w+$\"\n        $False\n    }\n}<\/code><\/pre>\n\n\n\n<p>I know this could have been a one-line command, but I wanted to add an explanation on why a function name would fail. The function takes a name and first tests if it matches the \"Word-Word\" pattern. If that passes, then the function tests if the \"verb\" is a standard verb. If so, the name passes the test.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/test-functionname.png\"><img loading=\"lazy\" decoding=\"async\" width=\"519\" height=\"321\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/test-functionname.png\" alt=\"\" class=\"wp-image-8697\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/test-functionname.png 519w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/test-functionname-300x186.png 300w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Exporting Functions<\/h2>\n\n\n\n<p>Time to put this all together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function Export-FunctionFromFile {\n    [cmdletbinding(SupportsShouldProcess)]\n    [alias(\"eff\")]\n    [OutputType(\"None\", \"System.IO.FileInfo\")]\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        [Parameter(HelpMessage = \"Specify the output path. The default is the same directory as the .ps1 file.\")]\n        [ValidateScript({ Test-Path $_ })]\n        [string]$OutputPath,\n        [Parameter(HelpMessage = \"Export all detected functions.\")]\n        [switch]$All,\n        [Parameter(HelpMessage = \"Pass the output file to the pipeline.\")]\n        [switch]$Passthru\n    )\n    Write-Verbose \"Starting $($MyInvocation.MyCommand)\"\n\n    #always create these variables\n    New-Variable astTokens -Force -WhatIf:$False\n    New-Variable astErr -Force -WhatIf:$False\n\n    if (-Not $OutputPath) {\n        #use the parent path of the file unless the user specifies a different path\n        $OutputPath = Split-Path -Path $Path -Parent\n    }\n\n    Write-Verbose \"Processing $path for functions\"\n    #the file will always be parsed regardless of WhatIfPreference\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\n    if ($functions.count -gt 0) {\n        Write-Verbose \"Found $($functions.count) functions\"\n        Write-Verbose \"Creating files in $outputpath\"\n        Foreach ($item in $functions) {\n            Write-Verbose \"Detected function $($item.name)\"\n            #only export functions with standard namees or if -All is detected.\n            if ($All -OR (Test-FunctionName -name $item.name)) {\n                $newfile = Join-Path -Path $OutputPath -ChildPath \"$($item.name).ps1\"\n                Write-Verbose \"Creating new file $newFile\"\n                Set-Content -Path $newFile -Value $item.ToString() -Force\n                if ($Passthru -AND (-Not $WhatIfPreference)) {\n                    Get-Item -Path $newfile\n                }\n            }\n            else {\n                Write-Verbose \"Skipping $($item.name)\"\n            }\n        } #foreach item\n    }\n    else {\n        Write-Warning \"No functions detected in $Path.\"\n    }\n    Write-Verbose \"Ending $($MyInvocation.MyCommand)\"\n} #end function<\/code><\/pre>\n\n\n\n<p>By default, the function will only export functions with standard names.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"992\" height=\"850\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function1.png\" alt=\"\" class=\"wp-image-8698\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function1.png 992w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function1-300x257.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function1-768x658.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function1-850x728.png 850w\" sizes=\"auto, (max-width: 992px) 100vw, 992px\" \/><\/a><\/figure>\n\n\n\n<p>If you look closely at the Verbose output you'll see that the detected non-standard functions are skipped. Although I can include all detected functions if I need to.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"589\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2-1024x589.png\" alt=\"\" class=\"wp-image-8699\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2-1024x589.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2-300x173.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2-768x442.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2-850x489.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function2.png 1032w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>The function allows me to specify an alternate location for the new files, although the default is the source file's parent directory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"777\" height=\"288\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function3.png\" alt=\"\" class=\"wp-image-8700\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function3.png 777w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function3-300x111.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/export-function3-768x285.png 768w\" sizes=\"auto, (max-width: 777px) 100vw, 777px\" \/><\/a><\/figure>\n\n\n\n<p>The source file is unaffected, although I suppose I could modify the function to delete code from the file. For now, I'll manually update the source file and any other module files to reflect the new function files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Try It<\/h2>\n\n\n\n<p>I have both functions in the same file which I dot source. Although I might add these functions to the PSScriptTools module at some point in the future. The export function assumes the file is a .ps1 or .psm1 file. You can see the parameter validation I'm using in the code. Give it a try and let me know what you think. The code should work in Windows PowerShell and PowerShell 7.x. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I write a PowerShell module, it typically includes more than one export function. Where you store your module functions is a great discussion topic and I don&#8217;t think there is necessarily one best practice for everyone. I think it might depend on the number and complexity of the functions. Are other people contributing code&#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: Exporting #PowerShell Functions to Files","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,224,534,540],"class_list":["post-8693","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-ast","tag-function","tag-powershell","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Exporting PowerShell Functions to Files &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"When working on a PowerShell module, I sometimes want to export functions in a single file to multiple files. This is how using the AST.\" \/>\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\/8693\/exporting-powershell-functions-to-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exporting PowerShell Functions to Files &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"When working on a PowerShell module, I sometimes want to export functions in a single file to multiple files. This is how using the AST.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-03T15:14:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-03T15:14:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.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=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Exporting PowerShell Functions to Files\",\"datePublished\":\"2021-12-03T15:14:36+00:00\",\"dateModified\":\"2021-12-03T15:14:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/\"},\"wordCount\":609,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/function-ast-member.png\",\"keywords\":[\"AST\",\"Function\",\"PowerShell\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/\",\"name\":\"Exporting PowerShell Functions to Files &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/function-ast-member.png\",\"datePublished\":\"2021-12-03T15:14:36+00:00\",\"dateModified\":\"2021-12-03T15:14:39+00:00\",\"description\":\"When working on a PowerShell module, I sometimes want to export functions in a single file to multiple files. This is how using the AST.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/function-ast-member.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/12\\\/function-ast-member.png\",\"width\":839,\"height\":498},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8693\\\/exporting-powershell-functions-to-files\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Exporting PowerShell Functions to Files\"}]},{\"@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":"Exporting PowerShell Functions to Files &#8226; The Lonely Administrator","description":"When working on a PowerShell module, I sometimes want to export functions in a single file to multiple files. This is how using the AST.","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\/8693\/exporting-powershell-functions-to-files\/","og_locale":"en_US","og_type":"article","og_title":"Exporting PowerShell Functions to Files &#8226; The Lonely Administrator","og_description":"When working on a PowerShell module, I sometimes want to export functions in a single file to multiple files. This is how using the AST.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-12-03T15:14:36+00:00","article_modified_time":"2021-12-03T15:14:39+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.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":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Exporting PowerShell Functions to Files","datePublished":"2021-12-03T15:14:36+00:00","dateModified":"2021-12-03T15:14:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/"},"wordCount":609,"commentCount":5,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png","keywords":["AST","Function","PowerShell","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/","name":"Exporting PowerShell Functions to Files &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png","datePublished":"2021-12-03T15:14:36+00:00","dateModified":"2021-12-03T15:14:39+00:00","description":"When working on a PowerShell module, I sometimes want to export functions in a single file to multiple files. This is how using the AST.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/function-ast-member.png","width":839,"height":498},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8693\/exporting-powershell-functions-to-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Exporting PowerShell Functions to Files"}]},{"@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":5319,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/5319\/a-classy-christmas-powershell-module\/","url_meta":{"origin":8693,"position":0},"title":"A Classy Christmas PowerShell Module","author":"Jeffery Hicks","date":"December 20, 2016","format":false,"excerpt":"Yesterday I showed you a class-based PowerShell script. My intention was to have a little bit of fun and teach you the basics of using a class. But what I gave you was really just the first step. If you wanted to create an actual tool around a class, you\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\/2016\/12\/christmas-wreath13.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":8724,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8724\/discovering-aliases-with-the-powershell-ast\/","url_meta":{"origin":8693,"position":1},"title":"Discovering Aliases with the PowerShell AST","author":"Jeffery Hicks","date":"December 15, 2021","format":false,"excerpt":"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\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\/find-alias-string.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/12\/find-alias-string.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":8343,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8343\/a-better-way-to-manage-powershell-functions\/","url_meta":{"origin":8693,"position":2},"title":"A Better Way to Manage PowerShell Functions","author":"Jeffery Hicks","date":"April 22, 2021","format":false,"excerpt":"Like many of you, I write a lot of PowerShell code. Much of it I use on a daily basis since I essentially spend my day at a PowerShell prompt. Also like many of you, I often assemble functions into a module. A module makes it easier to load the\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\/04\/psfunctioninfo-content.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/psfunctioninfo-content.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/psfunctioninfo-content.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/04\/psfunctioninfo-content.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":703,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/703\/export-function\/","url_meta":{"origin":8693,"position":3},"title":"Export-Function","author":"Jeffery Hicks","date":"July 19, 2010","format":false,"excerpt":"I love that you can do so much with PowerShell on the fly.\u00a0 For example, I don\u2019t need to launch a script editor to create a PowerShell function. I can do it right from the command prompt. PS C:\\> function tm {(get-date).ToLongTimeString()} PS C:\\> tm 12:41:27 PM As long as\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":[]},{"id":2287,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2287\/friday-fun-powershell-ise-function-finder\/","url_meta":{"origin":8693,"position":4},"title":"Friday Fun: PowerShell ISE Function Finder","author":"Jeffery Hicks","date":"May 11, 2012","format":false,"excerpt":"At the PowerShell Deep Dive in San Diego, I did a lightning session showing off something I had been working on. Sometimes I don't know what possesses me, but I felt the need for a better way to navigate my PowerShell scripts files that had many functions. Some files, especially\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/functionlist-gridview-252x300.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1502,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1502\/managing-virtualbox-with-powershell\/","url_meta":{"origin":8693,"position":5},"title":"Managing VirtualBox with PowerShell","author":"Jeffery Hicks","date":"June 13, 2011","format":false,"excerpt":"In my line of work I simply can't afford not to use virtualization, and I use just about all the major tools from time to time. But most of the time I rely on the free VirtualBox program from Oracle. One of the reasons I like it is it's relatively\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":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/06\/vbox.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8693","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=8693"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8693\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}