title: Using .NET objects within a Powershell (V5) class
body: <p>Below is the exact code that I am having trouble with.</p>
  <p>A brief description:</p>
  <p>I am trying to set up a PowerShell class that will hold objects of different types for easy access. I've done this numerous times in C#, so I thought it would be fairly straight forward. The types wanted are [System.Printing] and WMI-Objects.</p>
  <p>Originally I had tried to write the class directly to my PowerShell profile for easy usage, but my profile fails to load when I have to class code in it. Saying that it can’t find the type name &quot;System.Printing.PrintServer&quot;, or any other explicitly listed types.</p>
  <p>After that failed, I moved it to its own specific module and then set my profile to import the module on open. However, even when stored in its own module, if I explicitly list a .NET type for any of the properties, the entire module fails to load. Regardless of whether I have added or imported the type / dll.</p>
  <p>The specific problem area is this:</p>
  <pre><code>    [string]$Name
      [System.Printing.PrintServer]$Server
      [System.Printing.PrintQueue]$Queue
      [System.Printing.PrintTicket]$Ticket
      [System.Management.ManagementObject]$Unit
      [bool]$IsDefault
  </code></pre>
  <p>When I have it set to this, everything &quot;kind of&quot; works, but then all my properties have the _Object type, which is not helpful.</p>
  <pre><code>    [string]$Name
      $Server
      $Queue
      $Ticket
      $Unit
      $IsDefault
  
  
  Add-Type -AssemblyName System.Printing
  Add-Type -AssemblyName ReachFramework
  Class PrinterObject
  {
      [string]$Name
      [System.Printing.PrintServer]$Server
      [System.Printing.PrintQueue]$Queue
      [System.Printing.PrintTicket]$Ticket
      [System.Management.ManagementObject]$Unit
      [bool]$IsDefault
  
     PrinterObject([string]$Name)
      {
          #Add-Type -AssemblyName System.Printing
          #Add-Type -AssemblyName ReachFramework
          $this.Server = New-Object System.Printing.PrintServer -ArgumentList [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
          $this.Queue =  New-Object System.Printing.PrintQueue (($this.Server), ($this.Server.GetPrintQueues() |
          Where-Object {$_.Name -match $Name} | Select-Object -ExpandProperty Name))
  
          $this.Ticket = $this.Queue.UserPrintTicket
          $this.Unit = Get-WmiObject -Query &quot;SELECT * FROM Win32_Printer WHERE Name LIKE `&quot;%$Name%`&quot;&quot;
      }
  
      PrinterObject([string]$Name, [bool]$IsNetwork)
      {
          #Add-Type -AssemblyName System.Printing
          #Add-Type -AssemblyName ReachFramework
          if($IsNetwork -eq $true) {
          $this.Server = New-Object System.Printing.PrintServer (&quot;\\Server&quot;)
          $this.Queue =  New-Object System.Printing.PrintQueue (($this.Server), ($this.Server.GetPrintQueues() |
          Where-Object {$_.Name -match $Name} | Select-Object -ExpandProperty Name))
  
          $this.Ticket = $this.Queue.UserPrintTicket
          $this.Unit = Get-WmiObject -Query &quot;SELECT * FROM Win32_Printer WHERE Name LIKE `&quot;%$Name%`&quot;&quot;
          }
          else {
          $This.Server = New-Object System.Printing.PrintServer -argumentList [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
          $this.Queue =  New-Object System.Printing.PrintQueue (($this.Server), ($this.Server.GetPrintQueues() |
          Where-Object {$_.Name -match $Name} | Select-Object -ExpandProperty Name))
  
          $this.Ticket = $this.Queue.UserPrintTicket
          $this.Unit = Get-WmiObject -Query &quot;SELECT * FROM Win32_Printer WHERE Name LIKE `&quot;%$Name%`&quot;&quot; }
      }
      [void]SetPrintTicket([int]$Copies, [string]$Collation, [string]$Duplex)
      {
          $this.Ticket.CopyCount = $Copies
          $this.Ticket.Collation = $Collation
          $this.Ticket.Duplexing = $Duplex
          $this.Queue.Commit()
      }
  
      [Object]GetJobs($Option)
      {
              if($Option -eq 1) { return $this.Queue.GetPrintJobInfoCollection() | Sort-Object -Property JobIdentifier | Select-Object -First 1}
              else { return $this.Queue.GetPrintJobInfoCollection() }
      }
      static [Object]ShowAllPrinters()
      {
          Return Get-WmiObject -Class Win32_Printer | Select-Object -Property Name, SystemName
      }
  
  }
  </code></pre>