0

i want all the external users of a SharePoint 2013 site using powershell CSOM or C# CSOM as we dont have tenant admin rights to use Connect-SPOservice command. Is there any script in PnP or CSOM to get all external users. Please assist.

  • You've said you are dealing with SharePoint 2013, but then have tagged SPO and mentioned Connect-SPOservice. Please clarify if you're working with SPO or SharePoint server and remove the non-relevant tags/info. – sasfrog May 11 '19 at 0:51
1

Here's a way to do it: get the SiteUsers collection, and then filter that by the presence of the string #ext# in the LoginName property of each user.

The below sample assumes you're pointing to your CSOM library

# organise session etc.
$siteURL = "https://yourtenant.sharepoint.com/sites/yourcollection/yoursite"
$username =  "your-username@yourtenant.sharepoint.com" 

$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)

# Create a client context connection
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = $credentials

# get the SiteUsers collection
$users = $ctx.Web.SiteUsers
$ctx.Load($users)
$ctx.ExecuteQuery()

foreach ($u in $users) 
{
    if ($u.LoginName.Contains('#ext#')) 
    {
        Write-host $u.LoginName
    }
}

$ctx.Dispose()

Other properties in the user object that are returned in this collection might also be of interest, such as IsShareByEmailGuestUser, depending on what you want to know.

Note that this gives you users in the site collection; depending on your permissions configuration not all of these users may have access to the site context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.