title: Powershell memory error , groupmembership
body: <p>Could any please propose a better solution to export group membership based on filter .
  Everytime when I execute below script I get memory error although I have implemented filter but It's very difficult to filter base on L1, L2 userid because there are more than 30000 users.</p>
  
  <p>Also one more error I am getting while executing <code>"Cannot resolve directory object for the given identity powershell"</code>. </p>
  
  <p>ForEach-Object : <code>Exception of type 'System.OutOfMemoryException' was thrown.</code></p>
  
  <p>My requirement to export group membership is in below format only.It's extremely slow , take 4-5 hours to export even after filter used.</p>
  
  <p>My code:</p>
  
  <pre><code>$FilePath = "c:/Data/L_Accounts_Group_Membership_L1-L2.csv"
  $OuDomain = "OU=test,OU=abc,OU=com1,DC=com2,DC=com3,DC=com4"
  $users = Get-QADUser -SamAccountName [L1-L2]* -searchRoot $OuDomain -SizeLimit 0
  $users | Sort-Object SamAccountName | % {
      $user = $_ | Select-Object dn, sAMAccountName
      if ($lastDomain -eq $user.dn) {
          $lastDomain = ""
      } else {
          $lastDomain = $user.dn
      }
      $groups = Get-QADMemberOf $_ | Select-Object -expandProperty Name
      $groups | % {
          $user2 = New-Object -TypeName PsObject -Property @{
                  "Domain" = $lastDomain
                  "sAMAccountName" = $user.sAMAccountName
                  "Group" = $_
              }
          $user2
      }
  } | export-csv $FilePath
  </code></pre>