We can get the list of all external users in a SharePoint Online tenant using SharePoint Online Powershell cmdlet Get-SPOExternalUser and we can also find and list all the Office 365 guest users by using the Azure AD Powershell cmdlet Get-MsolUser. In this post, I am going to write script to export list of all the external user details to csv file.
Summary:
- Get all the External users using Get-SPOExternalUser cmdle
- Fetch all the Office 365 Guest users using Get-MsolUser cmdlet
- Export all the External users to CSV file
Get all the External users using Get-SPOExternalUser cmdlet
The below script list the external users from first page. You have to specify your SharePoint Online Admin Center url and Office 365 Admin Credentials to run the following commands.
#Connection to SharePoint Online $SPOAdminSiteUrl="https://<YourDomain>-admin.sharepoint.com/" $365Logon = Get-Credential Connect-SPOService -Url $SPOAdminSiteUrl -Credential $365Logon Get-SPOExternalUser -Position 0 -PageSize 50 | Select DisplayName,Email | FT
If you want to retrieve users from second page, you have to set the position as 1. The below command returns first 10 external users from the second page of the collection.
Get-SPOExternalUser -Position 1 -PageSize 10
You can also specify the parameter SiteUrl to retrieve external users only for a specific site.
Get-SPOExternalUser -Position 0 -PageSize 50 -SiteUrl <YourSiteUrl>
Fetch all the Office 365 External (Guest) users using Get-MsolUser cmdlet
The above command Get-SPOExternalUser will be very helpful if you have minimum number of external users. But it will be difficult if you have 100s of users as you have to fetch users page by page. So to overcome this problem, we can use the Azure AD Powershell cmdlet Get-MsolUser.
#Connection to Azure AD Module Import-Module MSOnline $365Logon = Get-Credential Connect-MsolService –Credential $365Logon Get-MsolUser -All | ? {$_.UserType -eq "Guest"} | Select DisplayName,SignInName | FT
The above command returns all the Office 365 external users (guest users).
You can also apply more where filter to get users from specific domain. The below command returns users only from the domain TestDomain.com.
Get-MsolUser -All | ? {$_.UserType -eq "Guest"} | ? {$_.SignInName -like "*TestDomain.com"}
Export all the External user details to CSV file
You can easily export the external user details to csv file by using the cmdlet Export-Csv.
Get-MsolUser -All | ? {$_.UserType -eq "Guest"} | Select DisplayName,SignInName | Export-CSV "C:\External-Users.csv" -NoTypeInformation -Encoding UTF8
Hi,
Great artcle.
I see we can get the site, who invited them and their email address, but is there a way to see what items in Sharepoint/OneDrive the external users have access to?
Also what rights do they have to each item.
TIA,
Joe