Forum Discussion
Tlu01
Jul 07, 2021Copper Contributor
How to pipe a result from one command to another for Office 365 powershell
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.
- 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
- 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- Tlu01Copper ContributorThanks, 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
- hansleroyIron ContributorHi,
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-the-following-powershell-command-in-a-batch-file?forum=winserverpowershell
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- Tlu01Copper Contributor
Thank you for the suggestion.