title: How to properly set function scope within a powershell module?
body: <h1>Background</h1>
  
  <p>I'd like all of my scripts and modules to be based on a template script which takes care of some common "housekeeping" tasks.</p>
  
  <p>Some of my modules are interdependent, and so I might load a module within another module, even if temporarily.</p>
  
  <p>However, because the code is templated, the basic initialization functions have the same name.
  In my mind, this shouldn't be an issue as long as I scope things properly, but it turns out it is an issue.</p>
  
  <h1>Demonstrating the issue</h1>
  
  <p>The demo below will have 1 script, which loads a module, which in turn loads another module.
  Each module has initialization code that runs when loading he module.</p>
  
  <ul>
  <li><strong>Update</strong>: updated the code to demonstrate a bit better why it's structured the way it is, although answer was already accepted.</li>
  </ul>
  
  <h2>module.level2.psm1</h2>
  
  <pre><code># module.level2.psm1
  # list of modules loaded by template code
  $script:LoadModule = $null
  
  # module housekeeping code - called from template code
  function ModuleSpecificInitCode {
      Write-Host "Level 2 Code"
  }
  
  function Level2ModuleFunction {
      Write-Host "This is why I imported Level 2 module"
  }
  
  # module housekeeping code - template code
  function TemplateInitCode {
      Write-Host "Level 2 Init"
      $LoadModule | % { Import-Module ".\$_.psm1" }
  }
  
  TemplateInitCode 
  try {
      ModuleSpecificInitCode 
  } catch {
  # Error handling
  }
  </code></pre>
  
  <h2>module.level1.psm1</h2>
  
  <pre><code># module.level1.psm1
  # list of modules loaded by template code
  $script:LoadModule = "module.level2"
  
  # module housekeeping code - called from template code
  function ModuleSpecificInitCode {
      Write-Host "Level 1 Code"
  }
  
  function Level1ModuleFunction {
      Write-Host "This is why I imported Level 1 module"
  }
  
  # module housekeeping code - template code
  function TemplateInitCode {
      Write-Host "Level 1 Init"
      $LoadModule | % { Import-Module ".\$_.psm1" }
  }
  
  TemplateInitCode 
  try {
      ModuleSpecificInitCode 
  } catch {
  # Error handling
  }
  </code></pre>
  
  <h2>test.ps1</h2>
  
  <pre><code># test.ps1
  Remove-Module module.level*
  Import-Module .\module.level1.psm1
  </code></pre>
  
  <p>When running test.ps1, the output I receive is:</p>
  
  <pre><code>PS&gt;.\test.ps1
  Level 1 Init
  Level 2 Init
  Level 2 Code
  Level 2 Code
  </code></pre>
  
  <h1>The Issue / My Question</h1>
  
  <p>The issue is the <strong>last line</strong>. Level 2 code is running instead of level 1 code.</p>
  
  <p>I've tried <code>local</code>, <code>private</code> and <code>script</code> as the <code>&lt;scope&gt;:</code> but whatever I do the <code>Level 1 Code</code> never runs.</p>
  
  <p>What am I missing here, why do all the modules seem to be running in the same namespace?</p>