I need to create a PowerShell script that connects to multiple SharePoint Online Sites and grants a user permissions imported from a CSV File

Rose Fletcher 30 Reputation points
2024-02-22T18:03:37.5433333+00:00

We are in a very large tenant migration project, and are migrating 8000 users from one Tenant to another. We have to reset permissions on SharePoint sites for the users. We have a permissions audit spreadsheet that lists out the users email, the SiteURL, their permissions to the site, whether from a group or individually, and then what level of access. The goal here is to use a CSV file, due the number of users. I am able to script out adding the users to the groups.
Import-Csv C:\MigrationFiles\UserstoGroups.csv | ForEach {Add-SPOUser -Group $.Group -LoginName $.LoginName -Site $_.Site} This works fine. However I am struggling doing individual permissions via a CSV import. I can do the commands individually: Connect-PNPOnline -URL (https://contso.sharepoint.com) -Interactive Set-PNPWebPermissions -User @contso.com -Add Role "Contribute" The problem I am having now is trying get this formatted to pull in multiple lines via a CSV file. My input file is as such: |URL User |Role||| | -------- | -------- | -------- | -------- | |https://contso.sharepoint.com/sites/Site1|@contso.com|Full Control|| |https://contoso.sharepoint.com/sites/Site4|@contso.com|Contribute|| |https://contso.sharepoint.com/sites/Site2|@contso.com|Read| |

Could someone help me with the correct syntax on the ForEach to run through the Connect PNPOnline and Set-PNPWebPermissions as two commands, reading each row? I am truly stuck here.

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,883 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,771 Reputation points
    2024-02-22T19:48:45.9766667+00:00

    I know nothing about SharePoint, but wouldn't this work?

    Import-Csv C:\junk\XXX.csv -Delimiter '|' |
        ForEach-Object{
           $site = ($_.URL -split '/')[-1]     # relative site name
           Set-PNPWebPermissions -Identity $site -User $_.User  -AddRole $_.Role
        }
    

    I'm assuming that your CSV looks like this:

    # URL|User|Role|X|Y|Z
    # https://contoso.sharepoint.com/sites/Site1|******@contso.com      |Full Control|          |          | |
    # https://contoso.sharepoint.com/sites/Site4|******@contso.com  |Contribute  |          |          | |
    # https://contoso.sharepoint.com/sites/Site2|******@contso.com    |Read        |          |          | |
    
    0 comments No comments

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.