title: Creating functions dynamically in a module in PowerShell
body: <p>Suppose I have the following code in a module (called MyModule.psm1, in the proper place for a module):</p>
  
  <pre><code>function new-function{
  
      $greeting='hello world'
      new-item -path function:\ -name write-greeting -value {write-output $greeting} -Options AllScope
      write-greeting
  }
  </code></pre>
  
  <p>After importing the module and running new-function I can successfully call the write-greeting function (created by new-function).  </p>
  
  <p>When I try to call the write-greeting function outside the scope of the new-function call, it fails because the function does not exist.  </p>
  
  <p>I've tried dot-sourcing new-function, but that doesn't help.  I've supplied the <code>-option Allscope</code>, but apparently that only includes it in child scopes.</p>
  
  <p>I've also tried explicitly following the new-item call with an export-modulemember write-greeting which doesn't give an error, but also doesn't create the function.</p>
  
  <p>I want to be able to create a function dynamically (i.e. via new-item because the contents and name of the function will vary based on input) from a function inside a module and have the newly created function available to call outside of the module. </p>
  
  <p>Specifically, I want to be able to do this:</p>
  
  <pre><code>Import-module MyModule
  New-Function
  write-greeting
  </code></pre>
  
  <p>and see "hello world" as output</p>
  
  <p>Any ideas?</p>