Granting permissions to sharepoint site via powershell

Sebastian Biały 40 Reputation points
2024-11-07T14:32:47.0933333+00:00

I would like to use powershell to grant permissions to multiple users via a csv/txt file to a sharepoint site.

I doesn't want to add them to any group, neither sharepoint group nor domain group. I want them to appear in the members tab (not site members) when I go to the site using sharepoint admin.

Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | Development
Windows for business | Windows Server | User experience | PowerShell
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 31,606 Reputation points Microsoft External Staff
    2024-11-08T08:36:45.6633333+00:00

    Hi @Sebastian Biały ,

    Welcome to Q&A forum!

    The members you are talking about are members of the Microsoft 365 group. You can use the following code to add users to members tab in batches.

    #Parameters
    $SiteURL = "https://domain.sharepoint.com/sites/site"
    $CSVFile = "C:\members.csv"
    
      
    Try {
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteURL -Interactive 
      
        #Get the Site
        $Site = Get-PnPSite -Includes GroupId
        #Get Data from CSV 
        $CSVData = Import-CSV -Path $CSVFile
        
           
        #Add users to the Site's associated Microsoft 365 Group Member
        ForEach($Row in $CSVData)
        {
        	Add-PnPMicrosoft365GroupMember -Identity $Site.GroupId -Users $Row.user
        	Write-host "Added Members to the Associated Microsoft 365 Group Successfully!" -f Green
    	
        }   
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message
    }
    

    For your reference:

    https://www.sharepointdiary.com/2018/05/add-members-to-office-365-group-using-powershell.html

    https://www.sharepointdiary.com/2020/04/sharepoint-online-powershell-to-grant-site-permissions-to-user.html

    Note: non-official, just reference.

    Hope this can help!


    If the answer is helpful, please click "Accept as 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.