is there a way to assign access to all the sites to any user in Sharepoint and export list of all sites?

Ritesh Sharma 361 Reputation points
2023-11-17T09:24:57.05+00:00

Hi, is there a way to assign any user access to all the sites in Sharepoint sites in one go, is there any permission settings available?

Also, how could we export all the site lists detail?

Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 31,601 Reputation points Microsoft External Staff
    2023-11-20T06:25:59.5133333+00:00

    Hi @Ritesh Sharma ,

    Welcome to Q&A forum!

    You can make it using PowerShell.

    1, Grants a direct Permission Level to the given user for all sites.

    #Parameters
    $TenantAdminURL="https://tenant-admin.sharepoint.com"
    $UserAccount = "******@tenant.com"
     
    #Get Credentials to Connect
    $Cred = Get-Credential
     
    Try {
        #Connect to Tenant Admin
        Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
     
    
    #Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
    $Sites = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0","SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
    
         
    #Loop through each Site Collection
    ForEach ($Site in $Sites)
    {
        Try {
            #Connect to the Site
            Connect-PnPOnline -Url $Site.Url -Credentials $Cred
    
            #Permission Level to Grant
            $PermissionLevel = "Contribute"
    
            #grant permission Level to the user
            Set-PnPWebPermission -User $UserAccount -AddRole $PermissionLevel
    
        }
        Catch {
            write-host -f Red "Error Adding User to the Site: $($Site.URL)" $_.Exception.Message
        }
    }
    
    }
    Catch {
    
    write-host -f Red "Error:" $_.Exception.Message
    
    }
    
    
    

    You can set the user who needs to add permissions by modifying the $UserAccount parameter, and set the permissions granted to the user by modifying the $PermissionLevel parameter.

    2, PowerShell script to retrieve a list of all sites and export it to a CSV file.

    #Config Variables
    $TenantSiteURL =  "https://tenant-admin.sharepoint.com/"
    $CSVFilePath = "C:\Temp\AllSitesData.csv"
     
    #Connect to Tenant Admin Site
    Connect-PnPOnline -Url $TenantSiteURL -Interactive
     
    #Get All Site collections data and export to CSV
    Get-PnPTenantSite -Detailed | Select Title, URL, Owner, LastContentModifiedDate, WebsCount, Template, StorageUsage | Export-Csv -path $CSVFilePath -NoTypeInformation
    

    You will get a csv file containing all site information.

    For more information, please refer to:

    https://www.sharepointdiary.com/2021/01/add-user-to-all-sharepoint-online-sites-using-powershell.html

    https://www.sharepointdiary.com/2016/02/get-all-site-collections-in-sharepoint-online-using-powershell.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link. 

    Hope this is helpful.

    Your recognition is my honor.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.