title: How can I use a PowerShell module in a script without leaving the module loaded in the user's session?
body: <p>I have a script I wish to use interactively from the PowerShell prompt. The script needs to use a local script module.</p>
  
  <p>I cannot see how to import/use the module such that it's not left loaded in the current session. </p>
  
  <p><strong>Example</strong></p>
  
  <p>A module (MyModule.psm1)...</p>
  
  <pre><code>function Test-Method
  {
      write-host "Test-Method invoked"
  }
  </code></pre>
  
  <p>... and a script (script.ps1)</p>
  
  <pre><code>Import-Module .\MyModule
  Test-Method
  </code></pre>
  
  <p>Now running the script at the PowerShell prompt ...</p>
  
  <pre><code>PS C:\temp&gt; Get-Module | % {$_.Name}
  Microsoft.PowerShell.Management
  Microsoft.PowerShell.Utility
  
  PS C:\temp&gt; .\script.ps1
  Test-Method invoked
  
  PS C:\temp&gt; Get-Module | % {$_.Name}
  Microsoft.PowerShell.Management
  Microsoft.PowerShell.Utility
  MyModule
  </code></pre>
  
  <p>How can my script import and use MyModule.psm1 without it being left loaded in the caller's current session? Bearing in mind that the call may have already imported the module and would not want it unloaded by the script (so simply removing the module at the completion of the script is not really good enough).</p>
  
  <p>I've considered dot-sourcing the module rather than importing it, but I want the module for the reasons covered in <a href="https://stackoverflow.com/questions/14882332/powershell-import-module-vs-dot-sourcing/14883042#14883042">PowerShell Import-Module vs Dot Sourcing</a></p>