Create a folder and set unique permission using PnP PowerShell!
Softree Technology > Blogs > PnP PowerShell > Create a folder and set unique permission using PnP PowerShell!

Create a folder and set unique permission using PnP PowerShell!

  • Posted by: Softree Technology

Introduction:

                        In this blog we are going to create a folder in Document Library and set unique permission to a user for this created folder using PnP Power Shell. The Administrator can set the required permission to a user depending on the organizational needs. Here we can use this script to set permission to a particular user. Please follow the below steps.

Step-1: – Run the ISE in administrative mode. Then declare variables named “sUrl”, “user”, “Folder Name” and “SiteRelativeURL” to store the respective values.

Step-2: – Connect to PnP online and get the credentials.

Connect-PnPOnline -Url $sUrl -Credentials (Get-Credential)

Step-3: – To create the folder in the document,use the PnP commands as shown in the code.

Add-PnPFolder -Name $FolderName -Folder $SiteRelativeURL -ErrorAction Stop

Step-4: – Copy and run the below codes to create a folder in Document and to set unique permission in the folder for a user.

#Config Variables

$sUrl = “https://contoso.sharepoint.com/sites/scriptEnable”

$user = “srikanta@contoso.onmicrosoft.com”

$FolderName= “Team Projects”

$SiteRelativeURL= “/Shared Documents” #Site Relative URL in which the folder will be created

Try {

    Connect-PnPOnline -Url $sUrl -Credentials (Get-Credential)

    # create folder using powershell

    Add-PnPFolder -Name $FolderName -Folder $SiteRelativeURL -ErrorAction Stop

    Write-host -f Green “New Folder ‘$FolderName’ Created Sucessfully!”

}

catch {

    write-host “Error: $($_.Exception.Message)” -foregroundcolor Red

}

try

{

Write-host -f Green “Adding unique permission to ‘$FolderName'”

$Path = “/sites/scriptEnable/Shared%20Documents/” + $FolderName

#Set folder permissions

Set-PnPfolderPermission -List $ListName -identity $Path -User $user -AddRole “Edit”

Write-host “Successfully added permission” -ForegroundColor Green

}

catch

{

write-host “Error: $($_.Exception.Message)” -foregroundcolor Red }

After running the script:

Folder “Team Project” is created successfully.

Now the permission is updated

Key Words:

1.How to create a Folder using PnP PowerShell?
2.How to set permission to a Folder using PnP Online?
3.Create a Folder and set user permission to it using PnP Online?

Author: Softree Technology

Leave a Reply