I want to create a full mailbox permission report and export it to a CSV in PowerShell.
$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
}
The gridview shows exactly what I want but I'm having a hard time getting it correctly exported to a CSV.

