Two PowerShell code snippets for getting a list of SharePoint users.
This first script takes your absolute site collection URL as input and prints out the login names of all the users within the SiteUsers property.
$siteColURL = "Your absolute site collection URL" $siteCollection = new-object Microsoft.SharePoint.SPSite($siteColURL) $spWeb = $siteCollection.openweb() $users = $spWeb.SiteUsers foreach($user in $users) { Write-Host " ------------------------------ " Write-Host "Site collection URL:", $siteColURL if($user.IsSiteAdmin -eq $true) { Write-Host "Site Admin: ", $user.LoginName } else { Write-Host "Site User: ", $user.LoginName } Write-Host " ------------------------------ " } $spWeb.Dispose() $siteCollection.Dispose()
The second script below builds on the first. It also takes the absolute URL of your site collection as input. In addition, it takes the name of a specific SharePoint list as an extra input.
$siteColURL = "Your absolute site collection URL" $listName = "Your SPList name" $siteCollection = new-object Microsoft.SharePoint.SPSite($siteColURL) $spWeb = $siteCollection.openweb() $spList = $spWeb.Lists[$listName] $users = $spWeb.SiteUsers foreach($user in $users) { Write-Host " ------------------------------ " Write-Host "Site collection URL:", $siteColURL if($spList.DoesUserHavePermissions([Microsoft.SharePoint.SPBasePermissions]::ViewListItems,$user) -eq $true) { Write-Host "Site User: ", $user.LoginName Write-Host "User Permissions: ", $spList.GetUserEffectivePermissions($user.LoginName) } Write-Host " ------------------------------ " } $spWeb.Dispose() $siteCollection.Dispose()
When you execute the code snippet above, it loops through all the SharePoint users in the SiteUsers property of the “openweb()”. Then it checks if each user has any permissions on the specified list.
If a user has any permissions on the list, the user’s login name is printed to screen along with the permissions they have.
Leave a Reply