Powershell – How to get AD user’s “Display name” instead of logon name (domain\userid) while exporting “send as” and “full” permissions

active-directoryexchangeexchange-2013powershellscripting

I have googled and created two scripts . (1), (2)

(1)First one is to export "Full Access" of a shared email box called "ap.cz"

Get-Mailbox ap.cz | Get-MailboxPermission | where {$_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} | Select Identity,User,@{Name='Access Rights';Expression={[string]::join(', ', $_.AccessRights)}} | Export-Csv \\myserver\c$\fulla.csv –NoTypeInformation

(2)Second one is to export "Send As" of a shared email box called "ap.cz"

Get-Mailbox ap.cz | Get-ADPermission | where { ($_.ExtendedRights -like “*Send-As*”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | Select Identity, User, Deny | Export-CSV \\myserver\c$\sendass.csv

Both scripts are working fine.

Output for (1) is similar to this

enter image description here

output for (2) is similar to this

enter image description here

But for both occasions I get "User logon name" in the format (domain\userid) where userid is a number in my organization.

But I need to get display name/full name instead of "User logon name" while exporting to csv..

I'm not an exchange admin or well versed in exchange/powershell, but i am checking/supporting overall IT infrastructure, and when a manager is asking for a list of names, for "send as" or "full access" for a certain mailbox, I have to export it using above scripts and re-convert "user logon name" to display name manually.

Can someone please advice how to change both scripts to display "full name/display name" instead of login name ? I have tried googlin, yet no luck..

Best Answer

$users.user.rawidentity will eliminate type casting and few more lines...

"Full Access" list

$users=Get-Mailbox ap.cz | Get-MailboxPermission | where {$_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} 
$ss=$users.user.rawidentity | Where-Object {$_ -notlike "s-1*"}

foreach ($item in $ss){ 
$b = $item.Split("\")
$c=$b.Split("}")[1]
Get-ADUser -identity $c -properties DisplayName | select DisplayName
}

"Send As" list

$users=Get-Mailbox ap.cz | Get-ADPermission | where { ($_.ExtendedRights -like “*Send-As*”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | Select User
$ss=$users.user.rawidentity | Where-Object {$_ -notlike "s-1*"}

foreach ($item in $ss){ 
$b = $item.Split("\")
$c=$b.Split("}")[1]
Get-ADUser -identity $c -properties DisplayName | select DisplayName
}