On a SharePoint Online site - what PowerShell code can read a CSV file > break inheritance on the document library folders > grant permissions to external users like this?

frob 4,216 Reputation points
2021-08-12T19:38:14.7+00:00

Hi there

On a SharePoint Online site - what PowerShell code can read the following CSV file > break inheritance on the document library folders > grant direct permissions to external users (not adding them to a group).

CSV file:

Site, DocLibPath, ExternalUserLogin, DesiredPermissions
http://domain.sharepoint.com/sites/site1, /DocLib1/ChildFolder1, john@Karima ben .com, Read Only
http://domain.sharepoint.com/sites/site1, /DocLib1/ChildFolder1, peter@Stuff .com, Contribute
http://domain.sharepoint.com/sites/site2, /DocLib1/ChildFolder2/ChildFolder3, jessica@Stuff .com, Contribute
http://domain.sharepoint.com/sites/site3, /DocLib2/ChildFolder2, mary@somevendor.com, Read Only
http://domain.sharepoint.com/sites/site1, /DocLib3/ChildFolder3, mary@somevendor.com, Read Only

Thank you so much.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,745 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. H Risbud 251 Reputation points
    2022-01-25T23:19:51.823+00:00

    Try this for internal users - not sure about external users...

    NOTE: this is not tested code... try it on your own

    $ImportFile = "<file location>"
    $P = Import-Csv -Path $ImportFile
    $P | ForEach-Object {
        #Site, DocLibPath, InternalUserLogin, DesiredPermissions
        $sID = $_.'Site'
        $DocLibPath = $_.'DocLibPath'
        $InternalUserLogin = $_.'InternalUserLogin'
        $DesiredPermissions = $_.'DesiredPermissions'
    
        #connect to site
        $destConn = Connect-PnPOnline -Url $sID -UseWebLogin -ReturnConnection
    
        $TargetLibrary = Get-PnPList -Identity $DocLibPath -Connection $destConn
    
        #$TargetLibrary - /sites/site1/DocLib1
        #$DocLibPath - /sites/site1/DocLib1/ChildFolder1
        #$InternalUserLogin - john.doe@abcdcorp.com
        #$DesiredPermissions - Read
        Set-PnPFolderPermission -List $TargetLibrary -Identity $DocLibPath -User $InternalUserLogin -AddRole $DesiredPermissions
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Elsie Lu_MSFT 9,771 Reputation points
    2021-08-13T09:18:14.407+00:00

    Hi @frob ,

    According to my research, due to SPO's security restrictions, I'm afraid that there is currently no such powershell that can grant different permissions to external users from csv.

    Reference:
    SharePoint Online: How to Add External User using PowerShell?
    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.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.