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.
1 Answer
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 = "[email protected]"
$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.