Introduction
Recently, I have been tasked with getting the site user's report in SharePoint sites in our organization Tenant. There are tools and other ways to get it. Here I have used PnP PowerShell version and the concept of PS Custom Object to achieve this task. In this article, we will see how to get the Site Users report using PnP PowerShell and PS Custom Object.
What is PS Custom Object?
This is a new property introduced in PowerShell 3.0 version and above, which helps in creating structured data, and with this data it makes it easier to
- Query the data
- Import and export the data
- Storing the data while working in a session
The details about PSCustomObject is nicely explained by Kevin in his blog and I would highly recommend checking it.
Pre-Requisites
Before proceeding you need to have the following configurations.
- Access to your organization tenant as SharePoint Admin or Global Admin or access to the sites as Site Collection admin where you want to get the site user's report
- PnP PowerShell module installed. Please investigate the references section on how to install the PowerShell module.
Steps
Please follow the below steps to get the site user's report.
Step 1
Connect to the SharePoint site
Connect-PnPOnline -Url $siteUrl -Interactive
Step 2
Get the Permission Groups in the SharePoint site.
$GroupNames = Get-PnPGroup
Step 3
For each user in Group get the required information and write to object. Here I am only getting Email, PrincipalType, Title, LoginName
$UserInfo = Get-PnPGroupMember -Identity $groupName | select Email,PrincipalType,Title,LoginName
If you are interested in getting additional properties you can also add them. By default, it gets the following properties, which can be found by querying a member with select * parameter.
$groupName = Get-PnPGroup -AssociatedOwnerGroup
$UserInfo = Get-PnPGroupMember -Identity $groupName | select *
Step 4
Export the object data into CSV.
Step 4: Export the object data into CSV.
$UserInfo | ForEach-Object{
$MemberEntity = [pscustomobject]@{
'Email' = $_.Email
'PrincipalType' = $_.PrincipalType
'Title' = $_.Title
'SiteUrl' = $siteUrl
'SiteName' =$siteTitle
'GroupName' = $groupName.Title
'LoginName' = $_.LoginName
}
$MemberEntity | Export-Csv -Path $OutputFile -Append -NoTypeInformation -Force
Validation
Below is the input format of the CSV file.
Below is the output CSV after the script run completes.
Below is the complete script
#Step1: Load the Site URLs into a collection
#Step2: For each site url get associated membersgroup and members
#Pending to get the members from OWners and Visitors
#Declaring variables
$CurrentTime = (Get - Date).ToString("yymmddhhmmss")
$OutputFile = "C:\Temp\OutPuts\SiteMembersInfo-" + $CurrentTime + ".csv"
#Add - Content - Path $OutputFile - Value "Email, SiteUrl, SiteName, IsEmailAuthenticationGuestUser, IsShareByEmailGuestUser, Title, PrincipalType"
$SiteUrlColl = Import - Csv - Path "C:Temp\Inputs\SiteURLCollection.csv"
#$SiteUrlColl = Get - PnPTenantSite | Select URL
try {
foreach($siteUrl in $SiteUrlColl.URL) {
Write - Host "Getting members for $siteUrl" - ForegroundColor Yellow
Connect - PnPOnline - Url $siteUrl - Interactive
$siteTitle = (Get - PnpWeb).Title
$GroupNames = Get - PnPGroup
foreach($groupName in $GroupNames) {
$UserInfo = Get - PnPGroupMember - Identity $groupName | select Email, PrincipalType, Title, LoginName
$UserInfo | ForEach - Object {
$MemberEntity = [pscustomobject] @ {
'Email' = $_.Email 'PrincipalType' = $_.PrincipalType 'Title' = $_.Title 'SiteUrl' = $siteUrl 'SiteName' = $siteTitle 'GroupName' = $groupName.Title 'LoginName' = $_.LoginName
}
$MemberEntity | Export - Csv - Path $OutputFile - Append - NoTypeInformation - Force
}
}
}
} catch {
Write - Host "Error: $($_.Exception.Message)" - ForegroundColor Red
}
Disconnect - PnPOnline
References