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,281 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, ******@somevendor.com, Read Only
http://domain.sharepoint.com/sites/site1, /DocLib3/ChildFolder3, ******@somevendor.com, Read Only

Thank you so much.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  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 - ******@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,806 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.


Your answer

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