Search code examples
powershellsharepointfile-permissionsonedrive

How to share a OneDrive file using powershell?


I have 3 files inside a subsolder of my OneDrive, say "Documens/TopFolder/SubFolder"

I can list 3 files inside this SubFolder using

Get-PnPFolder -FolderRelativeUrl "Documens/TopFolder/SubFolder"
File1.xlsx
File2.xlsx
File3.xlsx

Now I want to share these 3 files with 3 different users, say

  • File1 with User1@abc.com
  • File2 with User2@abc.com
  • File3 with User3@abc.com

I am a user in my organization, so I don't have admin access to share point. I just started learning powershell.

Questions are:

  1. How to get these 3 files into Get-PnPListItem?

  2. How to give permission for each file using windows powershell?

  3. How to get the annonymus weburl link for each file to share with individual user?


Solution

  • You could use Set-PnPListItemPermission to grant user permissions. Example:

    Set-PnPListItemPermission -List 'Documents' -Identity 1 -User 'user@contoso.com' -AddRole 'Contribute'
    

    https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/set-pnplistitempermission?view=sharepoint-ps

    Below articles may be helpful to you:

    https://www.sharepointdiary.com/2016/09/sharepoint-online-set-folder-permissions-powershell.html

    https://www.sharepointdiary.com/2017/11/sharepoint-online-grant-permission-to-list-item-using-powershell.html