SOLVED

How to pipe a result from one command to another for Office 365 powershell

Copper Contributor

I need to show the name of users with full permission access to a shared mailbox.  I found the script to export the user list, however it only shows the user ID in O365, not the Display name.  I found another script that can convert the user ID to Display name.  How can I link them together so I don't have to run two separate scripts to get the final result?

 

Script one to export out the User ID:

Get-MailboxPermission “Shared mailbox” | Where-Object { ($_.IsInherited -eq $False) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | Select-Object user | Export-Csv "sharedmailbox.csv"

 

Script two to convert the user ID to Display Name:

Import-Csv "sharedmailbox.csv" | foreach{Get-Mailbox -ResultSize Unlimited -Identity $_.user} | select *DisplayName* | Export-csv "sharedmailbox-Displayname.csv"

 

Thank you.

 

4 Replies
Hi,
You can run powershell scripts in a batch file, as explained in
https://social.technet.microsoft.com/Forums/en-US/e39a967d-f395-47b1-b5e0-c485b442bf48/how-do-i-run-...
That would give something like this (put the file in notepad, save as a .bat (NOT .TXT) file

powershell.exe -Command "(script 1)"
timeout 10
powershell.exe -Command "(script 2)"

Make sure the batch file is in a folder where you can write, so the CSV file can be saved.
Ah, and I put a pause of 10 seconds because the script might need some time to execute and save the file.

Kind regards

Hans
best response confirmed by Tlu01 (Copper Contributor)
Solution
No need to do this in multiple steps, you can use PowerShell's "calculated property" feature:

Get-MailboxPermission shared “Shared mailbox” | Where-Object { ($_.IsInherited -eq $False) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | select User,@{n="DisplayName";e={(Get-Recipient $_.User).displayName}} | Export-Csv "sharedmailbox.csv" -nti

Thank you for the suggestion.

Thanks, Vasil.

Still learning about PowerShell scripts.

I did have to remove the shared in the script for it to work.

Get-MailboxPermission “Shared mailbox” | Where-Object { ($_.IsInherited -eq $False) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | select User,@{n="DisplayName";e={(Get-Recipient $_.User).displayName}} | Export-Csv "sharedmailbox.csv" -nti
1 best response

Accepted Solutions
best response confirmed by Tlu01 (Copper Contributor)
Solution
No need to do this in multiple steps, you can use PowerShell's "calculated property" feature:

Get-MailboxPermission shared “Shared mailbox” | Where-Object { ($_.IsInherited -eq $False) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | select User,@{n="DisplayName";e={(Get-Recipient $_.User).displayName}} | Export-Csv "sharedmailbox.csv" -nti

View solution in original post