Thursday 1 March 2018

To get the Users, User ID, Title, Department for site collection and sub sites of site collections group users.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$WebURL="https://devglobal.deloitteresources.com/"
$ReportLocation = "C:\UserAnalysisRpt.csv"

#Get the Web
$Web = Get-SPWeb $WebURL

#Get User information list
$UserInfoList = $Web.SiteUserInfoList

"User Name`t  Department `t Title `t ID" | out-file "C:\UserAnalysisRpt.csv"

 #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=""
    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"]
            $UserIdIs = $UserInfo["ID"]
           
            #Send the output the report file
            $User.name + "`t" + $Department + "`t" + $JobTitle +"`t" + $UserIdIs>> $ReportLocation
        } 
 }
 Write-host "User analysis data has been Exported to $ReportLocation"

Result:


No comments:

Post a Comment