title: Export custom PowerShell object to CSV
body: <p>I want to create a full mailbox permission report and export it to a CSV in PowerShell.</p>
  
  <pre><code>$report = @()
  $mailboxes = Get-Mailbox -ResultSize unlimited -RecipientTypeDetails sharedmailbox
  
  foreach ($mailbox in $mailboxes)
  { 
      $permissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Get-MailboxPermission | Select-Object user, accessrights
      $users = $permissions.user
      $accessRights = $permissions.accessrights
      $sendAsPermissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Get-ADPermission | Where-Object {$_.extendedrights -eq "send-as"} | select -ExpandProperty user
      $sendOnBehalfPermissions = Get-Mailbox $mailbox.PrimarySmtpAddress | select -ExpandProperty grantsendonbehalfto
  
      $properties = @{
      Identity = $mailbox.PrimarySmtpAddress
      Users = ($permissions.user -join "`n")
      AccessRights = ($permissions.accessrights -join "`n")
      SendAs = ($sendAsPermissions -join "`n")  
      SendOnBehalf = ($sendOnBehalfPermissions -join "`n")
      }
  
      $mailboxObject = New-Object -TypeName psobject -Property $properties
  
      #$mailboxObject | select identity, users, accessrights | Export-Csv -Append -NoTypeInformation -Path c:\temp\test.csv
      $mailboxObject | select identity, users, accessrights, sendas, sendonbehalf | out-gridview
  }
  </code></pre>
  
  <p>The gridview shows exactly what I want but I'm having a hard time getting it correctly exported to a CSV.</p>
  
  <p><a href="https://i.stack.imgur.com/6FeRt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6FeRt.png" alt="Out-Gridview example"></a></p>