title: How to export a variable from a powershell module function?
body: <p>Please, observe:</p>
  
  <pre><code>C:\&gt; $TestVariable
  C:\&gt; $a
  C:\&gt; get-command Test-VariableExport
  
  CommandType     Name                                               Version    Source
  -----------     ----                                               -------    ------
  Function        Test-VariableExport                                0.0.0.1    Test
  
  
  C:\&gt; (get-command Test-VariableExport).ScriptBlock
  
      $script:TestVariable = Get-Date
  
  C:\&gt; Test-VariableExport
  C:\&gt; $TestVariable
  C:\&gt; function f() { $script:a = Get-Date }
  C:\&gt; f
  C:\&gt; $a
  
  Monday, April 15, 2019 2:48:59 PM
  
  
  C:\&gt; f
  C:\&gt; $a
  
  Monday, April 15, 2019 2:49:03 PM
  
  
  C:\&gt;
  </code></pre>
  
  <p>The same exact code when inside just a function works - the variable <code>a</code> is exported from the function to the outer scope. But when the function is part of a module as is the case of the <code>Test-VariableExport</code> function - the export does not work.</p>
  
  <p>How can I make it work for a module function?</p>