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

Powershell – To add site collection admins in bulk

Hello friends, in this article, we will see how we can assign site collection admin role to multiple user on multiple sites using powershell.

For this you need to summarize the site links and user email address in a csv format as shown below:

Powershell

<###############################################################
.SYNOPSIS
Script to set site collection administrator in SP Online
.DESCRIPTION
Sets site collection administrators for site collections in SP Online
.NOTE
SharePoint Online Management Shell needs to be installed in local
machine in order to execute the script
###############################################################>

Function Check-SiteExists($SiteUrl)
{
    $errpref = $ErrorActionPreference #save actual error preference
    $ErrorActionPreference = "silentlycontinue"

    return (Get-SPOSite -Identity $SiteUrl) -ne $null

    #return (Get-SPOSite -Identity $SiteUrl) -ne $null
    $ErrorActionPreference = $errpref #restore previous error preference    
}

Set-ExecutionPolicy Unrestricted -Force

$inputFilePath = "D:\Mayuresh\Sites1.csv"

$credential = Get-Credential

#Adding SharePoint Online Powershell module to the current session.
Import-Module MSOnline

#Connect-MsolService -Credential $credential

#Get-MsolUser

#Adding SharePoint Online Powershell module to the current session.
Import-Module Microsoft.Online.SharePoint.PowerShell

#Connects a SharePoint Online global administrator to a SharePoint Online connection
Connect-SPOService -url https://myclassbook-admin.sharepoint.com -Credential $credential

#import csv file containing site collection details
$sites = Import-Csv $inputFilePath
ForEach ($site in $sites)
{
    $SiteCollURL = $($site.Url)
    $SiteCollectionAdmin = $($site."Site Collection Admin")
   
    try
    {
        #Add Site collection Admin
        Set-SPOUser -site $SiteCollURL -LoginName $SiteCollectionAdmin -IsSiteCollectionAdmin $False
    }
    catch
    {
        Write-Host -ForegroundColor Red "Please check if the user "$SiteCollectionAdmin" is valid"
    }
}

#Disconnects the SharePoint Online service.
Disconnect-SPOService

Download: Assign site owner for multiple site

Powershell to unlock SharePoint Online site

Hello friends, below is the script that will help you to unlock your sharepoint online site:

#Change your tenant admin account below
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
$username = "[email protected]"
$password = ConvertTo-SecureString "[email protected]" –asplaintext –force   
#$password = convertto-securestring "YourPassword" -asplaintext -force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password

#Must be SharePoint Admin URL
$siteUrl = "https://myclassbook-admin.sharepoint.com"
Connect-SPOService -Url $siteUrl -Credential $cred
    
$site="https://myclassbook.sharepoint.com/teams/testsite"         
Set-SPOSite -Identity $site -LockState "Unlock" 

Download:

powershell to unlock single site