Powershell to get the list of Full Control Users on SharePoint

Powershell to get the list of Full Control Users on SharePoint

To get the full control users of a particular list of sites, first save the list of sites in csv format as below:

Powershell

Add-Type -Path "C:\Users\mayuresh\Desktop\Data\Old\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Users\mayuresh\Desktop\Data\Old\\Microsoft.SharePoint.Client.dll"
#Add-PSSnapin Microsoft.Sharepoint.Powershell


$admin = "[email protected]";
$password = Read-Host 'Enter Password' -AsSecureString
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin, $password)
Connect-SPOService -Url https://myclassbook-admin.sharepoint.com -credential [email protected]


#Setup the context
    
$webURL="https://myclassbook.sharepoint.com/teams/SPOSupportSite"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
$ctx.Credentials = $Credentials
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
#get the items from CSV
$inputFilePath = "D:\Mayuresh\MissingOwners\Book2.csv"
$itemCol = Import-Csv $inputFilePath


	$lists = $ctx.web.Lists
	$list = $lists.GetByTitle("SiteUrls")
	$ExslistItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
	$ctx.load($ExslistItems)
	$ctx.executeQuery()

    ForEach($EstlistItem in $ExslistItems)
    {
    $list.GetItemById($EstlistItem.Id).DeleteObject()  
    $ctx.executeQuery() 
    }
    Write-Host $itemCol -ForegroundColor Cyan
	ForEach ($item in $itemCol)
    {
        $siteURL = $item.SiteUrls
        Write-Host $siteURL -ForegroundColor Cyan

        #Get all Groups from the site permissions
        $sitegroups = Get-SPOSiteGroup -Site $siteURL

        #Get Group info and members that have site owners permissions
        foreach ($sitegroup in $sitegroups)
        {
            #$i = 0
            foreach($role in $sitegroup.Roles)
            {
            try{
                if ( $role.Contains(“Full Control”) )
                {
                    $listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation  
                    $listItem = $list.AddItem($listItemInfo)  
                    $listItem["Title"] = $siteURL  
                    Write-Host $sitegroup.Title -ForegroundColor “Yellow”
                    Write-Host $sitegroup.Users -ForegroundColor “Blue”
                    foreach($user in $sitegroup.Users)
                    {
                    	$listItem["FullControlUsers"]=$listItem["FullControlUsers"]+"; "+$user;
                    }
                    $listItem["FullControlUsers"]=$listItem["FullControlUsers"].TrimStart('; ');
                    $listItem.Update();
                    $ctx.executeQuery() 
                }
             }
             catch{
	            write-host "$($_.Exception.Message)" -foregroundcolor red
             }
            }
        }
    }

Download: GetFullControlUsers

Leave a Reply