Hi there
What PowerShell code can list all SharePoint Online content (sites/lists/libraries/items etc.) shared with external users, with URL and the external user's email?
Thanks.
Hi there
What PowerShell code can list all SharePoint Online content (sites/lists/libraries/items etc.) shared with external users, with URL and the external user's email?
Thanks.
@frob-0826
1.Use below PowerShell.
#Import SharePoint Online Management Shell
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
#Config Parameters
$AdminSiteURL="https://tenant -admin.sharepoint.com"
$ReportOutput ="C:\ExternalUsersRpt.csv"
#Get Credentials to connect
$Cred = Get-Credential
#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $Cred
#Get all Site Collections
$SitesCollection = Get-SPOSite -Limit ALL
$ExternalUsers=@()
#Iterate through each site collection
ForEach($Site in $SitesCollection)
{
Write-host -f Yellow "Checking Site Collection:"$Site.URL
#Get All External users of the site collection
$ExtUsers = Get-SPOUser -Limit All –Site $Site.URL | Where {$_.LoginName -like "*#ext#*" -or $_.LoginName -like "*urn:spo:guest*"}
If($ExtUsers.count -gt 0)
{
Write-host -f Green "Found $($ExtUsers.count) External User(s)!"
$ExternalUsers += $ExtUsers
}
}
#Export the Data to CSV file
$ExternalUsers | Export-Csv -Path $ReportOutput –NoTypeInformation
2.Use below PnP PowerShell.
#Parameter
$Domain = "tenant" #Domain Name in SharePoint Online. E.g. https://tenant.SharePoint.com
$CSVFile = "C:\ExternalSharing.csv"
#Frame Tenant URL and Tenant Admin URL
$TenantURL = "https://$Domain.SharePoint.com"
$TenantAdminURL = "https://$Domain-Admin.SharePoint.com"
#Delete the Output report file if exists
If (Test-Path $CSVFile) { Remove-Item $CSVFile }
#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -UseWebLogin
#Get All Site collections with External sharing enabled - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL'" | Where {$_.SharingCapability -ne "Disabled"}
#Iterate through all site collections
$Sites | ForEach-Object {
Write-host "Getting External Users of Site:"$_.URL -f Yellow
#Connect to each site collection
$SiteConn = Connect-PnPOnline -Url $_.URL -UseWebLogin -ReturnConnection
$ExternalUsersData = @()
#Get all External Users of the site collection
$ExternalUsers = Get-PnPUser -Connection $SiteConn| Where {$_.LoginName -like "*#ext#*" -or $_.LoginName -like "*urn:spo:guest*"}
Write-host "`tFound '$($ExternalUsers.count)' External users" -f Green
#Collect Data
ForEach($User in $ExternalUsers)
{
$ExternalUsersData += New-Object PSObject -Property ([ordered]@{
SiteName = $_.Title
SiteURL = $_.URL
UserName = $User.Title
Email = $User.Email
})
}
#Export Documents Inventory to CSV
$ExternalUsersData | Export-CSV $CSVFile -NoTypeInformation -Append
Disconnect-PnPOnline -Connection $SiteConn
}
Write-host "External Users Report Generated Successfully!" -f Magenta
If an Answer is helpful, please click "Accept Answer" and upvote it
Note: Please follow the steps in our email-notifications.htmldocumentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hi michev
Not for OneDrive, I needed a list of external users for SharePoint Online content (lists/libraries/sites etc.).
Please do let me know if you have.
Thanks.
@frob-0826
You could use below PowerShell to get shared contents, external users and their emails.
Param(
[Parameter(Mandatory=$True)]
[string]$AdminCenterUrl ,
[Parameter(Mandatory=$True)]
[string]$CSVFilePath
)
Write-Host "Please enter your credentials to connect to SharePoint Online Admin Center"
Try{
if (-not (Get-Module -Name "Microsoft.Online.SharePoint.Powershell")) {
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking #Loading SPO module if it already loaded
}
connect-sposervice $AdminCenterUrl
}
Catch [Exception]{
write-host "Sorry, something went wrong while connecting to the Admin Center.. Detailed error message: $($_.Exception.Message)" -foregroundcolor Red
}
Try{
$properties=@{SiteCollectionUrl='';DisplayName='';OriginalEmail='';AcceptedAs='';JoinDate='';InvitedBy=''};
$externalUsers=@();
Foreach($site in Get-SPOSite){
write-host "Getting external users at $($site.Url)"
$users = Get-SPOExternalUser -siteurl $site.Url
foreach($user in $users){
write-host "External user found, Name: $($user.DisplayName)" -ForegroundColor Yellow
$externalUser = New-Object -TypeName PSObject -Property $properties;
$externalUser.SiteCollectionUrl = $site.Url
$externalUser.DisplayName = $user.DisplayName
$externalUser.OriginalEmail = $user.Email
$externalUser.AcceptedAs = $user.AcceptedAs
$externalUser.JoinDate = $user.WhenCreated
$externalUser.InvitedBy = $user.InvitedBy
$externalUsers+= $externalUser
}
}
}
Catch [Exception]{
write-host "Sorry, something went wrong while checking the external users.. Detailed error message: $($_.Exception.Message)" -foregroundcolor Red
}
try{
$externalUsers | select SiteCollectionUrl, DisplayName , OriginalEmail,AcceptedAs, JoinDate , InvitedBy | Export-Csv -Path $CSVFilePath
write-host "File has been exported successfully, click enter to exit" -ForegroundColor Green
read-host
}
catch [Exception]{
write-host "Sorry, something went wrong while exporting info to csv file.. Detailed error message: $($_.Exception.Message)" -foregroundcolor Red
}
Here’s a reference for you.
https://o365reports.com/2021/03/23/audit-external-user-file-access-in-sharepoint-online-using-powershell/
If an Answer is helpful, please click "Accept Answer" and upvote it
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hi Emily,
It lists only some of the users and not all. Any thoughts, please?
Thanks.
@frob-0826
It is caused by that the Get-SPOExternalUser cmdlet has a limitation of returning first 50 users only.
I add a new answer, please check.
14 people are following this question.