SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-2521581%22%20slang%3D%22en-US%22%3EHow%20to%20pipe%20a%20result%20from%20one%20command%20to%20another%20for%20Office%20365%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2521581%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20show%20the%20name%20of%20users%20with%20full%20permission%20access%20to%20a%20shared%20mailbox.%26nbsp%3B%20I%20found%20the%20script%20to%20export%20the%20user%20list%2C%20however%20it%20only%20shows%20the%20user%20ID%20in%20O365%2C%20not%20the%20Display%20name.%26nbsp%3B%20I%20found%20another%20script%20that%20can%20convert%20the%20user%20ID%20to%20Display%20name.%26nbsp%3B%20How%20can%20I%20link%20them%20together%20so%20I%20don't%20have%20to%20run%20two%20separate%20scripts%20to%20get%20the%20final%20result%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%20one%20to%20export%20out%20the%20User%20ID%3A%3C%2FP%3E%3CP%3EGet-MailboxPermission%20%E2%80%9CShared%20mailbox%E2%80%9D%20%7C%20Where-Object%20%7B%20(%24_.IsInherited%20-eq%20%24False)%20-and%20-not%20(%24_.User%20-like%20%E2%80%9CNT%20AUTHORITY%5CSELF%E2%80%9D)%20%7D%20%7C%20Select-Object%20user%20%7C%20Export-Csv%20%22sharedmailbox.csv%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%20two%20to%20convert%20the%20user%20ID%20to%20Display%20Name%3A%3C%2FP%3E%3CP%3EImport-Csv%20%22sharedmailbox.csv%22%20%7C%20foreach%7BGet-Mailbox%20-ResultSize%20Unlimited%20-Identity%20%24_.user%7D%20%7C%20select%20*DisplayName*%20%7C%20Export-csv%20%22sharedmailbox-Displayname.csv%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2521581%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2521671%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20pipe%20a%20result%20from%20one%20command%20to%20another%20for%20Office%20365%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2521671%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3EYou%20can%20run%20powershell%20scripts%20in%20a%20batch%20file%2C%20as%20explained%20in%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fen-US%2Fe39a967d-f395-47b1-b5e0-c485b442bf48%2Fhow-do-i-run-the-following-powershell-command-in-a-batch-file%3Fforum%3Dwinserverpowershell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fen-US%2Fe39a967d-f395-47b1-b5e0-c485b442bf48%2Fhow-do-i-run-the-following-powershell-command-in-a-batch-file%3Fforum%3Dwinserverpowershell%3C%2FA%3E%3CBR%20%2F%3EThat%20would%20give%20something%20like%20this%20(put%20the%20file%20in%20notepad%2C%20save%20as%20a%20.bat%20(NOT%20.TXT)%20file%3CBR%20%2F%3E%3CBR%20%2F%3Epowershell.exe%20-Command%20%22(script%201)%22%3CBR%20%2F%3Etimeout%2010%3CBR%20%2F%3Epowershell.exe%20-Command%20%22(script%202)%22%3CBR%20%2F%3E%3CBR%20%2F%3EMake%20sure%20the%20batch%20file%20is%20in%20a%20folder%20where%20you%20can%20write%2C%20so%20the%20CSV%20file%20can%20be%20saved.%3CBR%20%2F%3EAh%2C%20and%20I%20put%20a%20pause%20of%2010%20seconds%20because%20the%20script%20might%20need%20some%20time%20to%20execute%20and%20save%20the%20file.%3CBR%20%2F%3E%3CBR%20%2F%3EKind%20regards%3CBR%20%2F%3E%3CBR%20%2F%3EHans%3C%2FLINGO-BODY%3E
New 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 (New 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