Get User Department, Job Title from User Information List of SharePoint using PowerShell
Requirement: From a business requirement, we need to generate a report for department-wise users of an Intranet portal site. Say, How many users from the “Sales” department have access to the SharePoint Intranet portal?
Solution: Either query the user profiles store or user information list to get the user profile properties such as Department, Job title, etc.
PowerShell to query User Information List in SharePoint
Here is how to get data from the user information list in SharePoint using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Parameters
$WebURL="https://portal.crescent.com"
$ReportLocation = "C:\UserAnalysisRpt.csv"
#Get the Web
$Web = Get-SPWeb $WebURL
#Get User information list
$UserInfoList = $Web.SiteUserInfoList
#Get all Groups of the web and Iterate through
Foreach ($Group in $Web.Groups)
{
#Get Permission Levels Applied to the Group
$RoleAssignment = $Web.RoleAssignments.GetAssignmentByPrincipal($Group)
$RoleDefinitionNames=[string]::Empty
foreach ($RoleDefinition in $RoleAssignment.RoleDefinitionBindings)
{
$RoleDefinitionNames+=$RoleDefinition.Name+";"
}
#Array to Hold Result - PSObjects
$ResultCollection = @()
"Group Name: $($Group.name) : Permissions: $($RoleDefinitionNames)" >> $ReportLocation
#Iterate through Each User in the group
foreach ($User in $Group.users)
{
#Get the User details from UIL
$UserInfo = $UserInfoList.GetItemById($User.ID)
$Department = $UserInfo['Department']
$JobTitle = $UserInfo["JobTitle"]
#Send the output the report file
$User.name + "`t" + $user.Email + "`t" + $Department + "`t" + $JobTitle >> $ReportLocation
}
}
Write-host "User analysis data has been Exported to $ReportLocation"
If you want to go for querying user profile properties from the profile store, use my other post: Query User Profile Properties using PowerShell in SharePoint 2013