{"id":1385,"date":"2012-03-28T15:46:46","date_gmt":"2012-03-28T13:46:46","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1385"},"modified":"2015-09-08T20:36:23","modified_gmt":"2015-09-08T18:36:23","slug":"powershell-export-modulemember-vs-export-keys-in-manifest","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/powershell-export-modulemember-vs-export-keys-in-manifest\/","title":{"rendered":"PowerShell Export-ModuleMember vs Export keys in manifest"},"content":{"rendered":"<p>In PowerShell there are two ways to export functions, cmdlets, variables and aliases from a script module file for use by the calling context:<br \/>\n1. By using Export-ModuleMember and specify what resources need to be exported like:<br \/>\nExport-ModuleMember -Function  or Export-ModuleMember -Variable<\/p>\n<p>2. By using a module manifest and specifiy what resources need to be exported like:<br \/>\n@{<br \/>\n&#8230;.<br \/>\nFunctionsToExport = &#8216; VariablesToExport = &#8221;<br \/>\n&#8230;.<br \/>\n}<\/p>\n<p>By seeing this I got a little confused on which method to use and why: Export-ModuleMember or the manifest way, so it was time to dig into this matter.<br \/>\nBy creating a small module, a manifest and a script file the different options can be tested.<\/p>\n<h4>Module (psm1)<\/h4>\n<p>In the module three functions, two with an alias and two variables are defined and exported:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$msgText = 'Hello world!'\r\n$anotherVariable = 'more here'\r\n\r\nfunction Say-HelloWorld() { Write-Host $msgText }\r\nfunction Calc-Numbers(&#x5B;int] $a,&#x5B;int] $b) { $a + $b }\r\nfunction I-Am-Private() { Write-Host &quot;private&quot; }\r\n\r\nSet-Alias Add Calc-Numbers\r\nSet-Alias Hello Say-HelloWorld\r\n\r\nExport-ModuleMember -Function Say-HelloWorld, Calc-Numbers\r\nExport-ModuleMember -Variable msgText, anotherVariable\r\nExport-ModuleMember -Alias Hello, Add\r\n<\/pre>\n<h4>Manifest (psd1)<\/h4>\n<p>The export variables of the manifest are (default) set to &#8216;*&#8217;:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Functions to export from this module\r\nFunctionsToExport = '*'\r\n\r\n# Cmdlets to export from this module\r\nCmdletsToExport = '*'\r\n\r\n# Variables to export from this module\r\nVariablesToExport = '*'\r\n\r\n# Aliases to export from this module\r\nAliasesToExport = '*'\r\n<\/pre>\n<h4>Script (ps1)<\/h4>\n<p>The script file imports the module and displays information about the module. This is an excellent way to test what resources are exported:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nImport-Module ExportFunctions -Force\r\nGet-Module -Name ExportFunctions | fl\r\n<\/pre>\n<h4>Tests<\/h4>\n<p>Running this script results in the following output:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1388 alignnone\" title=\"PSModules01\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules01.png\" alt=\"\" width=\"457\" height=\"166\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules01.png 457w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules01-300x108.png 300w\" sizes=\"auto, (max-width: 457px) 100vw, 457px\" \/><\/a><\/p>\n<p>As can be seen two functions, the aliases and variables are exported as defined in Export-ModuleMember in the module.<\/p>\n<p>The Export-ModuleMembers lines are now removed to test the default settings in the manifest file (all &#8216;*&#8217; for the export keys). Running the same script now results in:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1389\" title=\"PSModules02\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules02.png\" alt=\"\" width=\"532\" height=\"170\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules02.png 532w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules02-300x95.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>All the functions are exported, but none of the alias or variables. At first sight this seems a bit strange, because the generated comment of the export keys of the manifest say &#8216; to export from this module&#8217;. This seems only the case for the functions.<\/p>\n<p><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd878297%28v=VS.85%29.aspx\">TechNet <\/a>documentation for FunctionsToExport key:<\/p>\n<blockquote><p>Specifies the functions that the module exports (wildcard characters are permitted) to the caller\u2019s session state. By default, all functions are exported. You can use this key to restrict the functions that are exported by the module&#8230;<\/p><\/blockquote>\n<p>And the TechNet documentation for VariableToExport key:<\/p>\n<blockquote><p>Specifies the variables that the module exports (wildcard characters are permitted) to the caller\u2019s session state. By default, all variables are exported. You can use this key to restrict the variables that are exported by the module&#8230;<\/p><\/blockquote>\n<p>The statement don&#8217;t differ, but the behavior is.<\/p>\n<h5>The &#8216;*&#8217; at the FunctionsToExport key means the restriction is:<\/h5>\n<p><span style=\"text-decoration: underline;\">If Export-ModuleMember -Function in the module is used, the functions listed there are exported:<\/span><br \/>\nIn module:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Export-ModuleMember -Function Say-HelloWorld, Calc-Numbers<\/pre>\n<p>In manifest:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">FunctionsToExport = '*'<\/pre>\n<p>Result:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1390\" title=\"PSModules03\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03.png\" alt=\"\" width=\"412\" height=\"179\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03.png 412w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03-300x130.png 300w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"> If Export-ModuleMember -Function is NOT used in the module and in manifest &#8216;*&#8217; is used all functions are exported: <\/span><br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1391\" title=\"PSModules03a\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03a.png\" alt=\"\" width=\"532\" height=\"170\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03a.png 532w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules03a-300x95.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<h5>The &#8216;*&#8217; at the VariablesToExport key means: By default none of the variables are exported, unless variables are exported by the Export-ModuleMember in the module.<\/h5>\n<p>In module: No Export-ModuleMember -Variable<br \/>\nIn manifest:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">VariablesToExport = '*'<\/pre>\n<p>Result:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1392\" title=\"PSModules04\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules04.png\" alt=\"\" width=\"421\" height=\"175\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules04.png 421w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules04-300x124.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/a><\/p>\n<p>In module: No Export-ModuleMember -Variable<br \/>\nIn manifest:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">VariablesToExport = 'anotherVariable'<\/pre>\n<p>Result:<br \/>\n<a href=\"..\/wp-content\/uploads\/2012\/03\/PSModules04.png\"><img loading=\"lazy\" decoding=\"async\" title=\"PSModules04\" src=\"..\/wp-content\/uploads\/2012\/03\/PSModules04.png\" alt=\"\" width=\"421\" height=\"175\" \/><\/a><\/p>\n<p>In module:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Export-ModuleMember -Variable msgText, anotherVariable<\/pre>\n<p>In manifest:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">VariablesToExport = '*'<\/pre>\n<p>Result:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules05.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1393\" title=\"PSModules05\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules05.png\" alt=\"\" width=\"424\" height=\"173\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules05.png 424w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules05-300x122.png 300w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/a><\/p>\n<p>In module:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Export-ModuleMember -Variable msgText, anotherVariable<\/pre>\n<p>In manifest:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">VariablesToExport = 'anotherVariable'<\/pre>\n<p>Result:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1394\" title=\"PSModules06\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules06.png\" alt=\"\" width=\"423\" height=\"176\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules06.png 423w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2012\/03\/PSModules06-300x124.png 300w\" sizes=\"auto, (max-width: 423px) 100vw, 423px\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p>The export keys in the manifest can be seen as an export override of the Export-ModuleMember used in a module. When testing this behavior with functions alone it can be confusing &#8211; at least it confused me &#8211; which method to use &#8211; manifest export key or Export-ModuleMember &#8211; to restrict function exports. When trying to control the exports of variables and aliases it all comes clear: export them in the module at all times!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PowerShell there are two ways to export functions, cmdlets, variables and aliases from a script module file for use by the calling context: 1. By using Export-ModuleMember and specify what resources need to be exported like: Export-ModuleMember -Function or &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/powershell-export-modulemember-vs-export-keys-in-manifest\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,8],"tags":[43],"class_list":["post-1385","post","type-post","status-publish","format-standard","hentry","category-powershell","category-sharepoint-2010","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1385"}],"version-history":[{"count":12,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1385\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1385\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}