Search code examples
powershellpowershell-2.0disposeidisposablepsobject

How do I/do I need to dispose of custom PSObjects?


Do I need to dispose of the object returned from this function? I don't see a Dispose method on a PSObject, but that doesn't necessarily mean the object can't/shouldn't be disposed of. I've searched google, and can't find anything related to disposing of a PSObject object.

function MakeDBConnectInfoObject(
   [string] $DBDestServer,
   [string] $DBDestDB,
   [string] $DBDestUserName,
   [string] $DBDestPassword
) {
   $DBConnectInfo = new-object -typename psobject -property @{
      DBDestServer = $DBDestServer
      DBDestDB = $DBDestDB
      DBDestUserName = $DBDestUserName
      DBDestPassword = $DBDestPassword
   }

   return $DBConnectInfo
}

Solution

  • No, you do not have to dispose of it because [PSObject] does not implement [System.IDisposable].

    You can test this with the -is operator:

    $object -is [System.IDisposable]