What PowerShell can list all SharePoint Online content shared with external users, with URL and the external users' email?

frob 4,261 Reputation points
2021-04-30T21:27:11.847+00:00

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.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Emily Du-MSFT 51,836 Reputation points Microsoft External Staff
    2021-05-04T10:39:25.837+00:00

    @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  
    

    Reference:
    https://www.sharepointdiary.com/2017/11/sharepoint-online-find-all-external-users-using-powershell.html


    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.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2021-05-02T11:03:03.267+00:00

  2. Emily Du-MSFT 51,836 Reputation points Microsoft External Staff
    2021-05-03T08:52:38.847+00:00

    @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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.