title: Powershell module initialization
body: <p>Does PowerShell call any initialization code when a module is loaded?</p>
  
  <p>I am looking for something like a Perl BEGIN block, or a constructor.</p>
  
  <p>Both NEW-MODULE and IMPORT-MODULE will return a PSCustomObject. I am trying to encapsulate a custom object in a module to avoid lengthy code in scripts. One method that tests well in open code is:</p>
  
  <pre><code>$m = new-module -scriptblock {
  New-Object PSCustomObject | 
      Add-Member NoteProperty -name person -value Frodo -passthru | 
      Add-Member ScriptMethod Who { $this.person }  -passthru |
      Add-Member ScriptMethod Mod { 
          param($x)
          $this.person = $x
          } -passthru
  } -ascustomobject -returnresult
  </code></pre>
  
  <p>Ideally I would like to drop this code into a module and use something like:</p>
  
  <pre><code>$MyObj = Import-Module -Name ".\MyPackage" -AsCustomObject
  </code></pre>
  
  <p>and have MyObj be a handle to an object the same as the first snippet provides.</p>
  
  <p>Suggestions appreciated.</p>