Jump to content

PNP.Powershell Script to Create folders and set permissions via sharepoint/Teams


Recommended Posts

Posted

As part of our transition im lookign for the following

 

Script to create folders based on a CSV ( already did this which is fine )

 

The part im struggling is , I want it to read the directory name and assign permissions based on this . So say the folder created is called "5647" i want it to assign the username "5647" as the user permissions.

 

I have a similar script in windows using powershell which works great , But cannot work this out using PNP.Powershell.

 

Thanks

2097

Posted

So took me a bit , But managed to modify a script i found that didnt work.

 

This script will read from a CSV.

 

Will create all the folders,as indiciated by line 1 in the CSV

 

It will then add permissions for the users to write direct to their folder via their email address in line 2.

 

It will Remove existing permissions ( Remove kids access to everyones folder ) , and add the "Staff" group email for full access .

 

 

 

 

#1
$siteurl = "https://SITE.sharepoint.com/sites/StudentDrive"                
#2
$listname = "Shared Documents/General/Business/Uploads"          
#3
$access = "full control"
#4
$Folders = Import-Csv "D:\SubFoldersPermtest.csv"    
#6
$FullPath = "/sites/StudentDrive/Shared Documents/General/Business/Uploads"
#7
$Staff = "[email protected]"


#----------START OF SCRIPT DO NOT MODIFY----------#
connect-PnPOnline –Url $siteurl -Interactive
#–Credentials (Get-Credential)
foreach ( $entry in $Folders )
{

$foldername = $entry.name
$emailaddress = $entry.EmailAddress


#Create Folder
Add-PnPFolder -Name $foldername -Folder $listname 

#get folder properites from list
$listItem = Get-PnPListItem -List $listname -FolderServerRelativeUrl $FullPath -Query "$($foldername)"

Write-Host "setting folder up for $foldername"

#set permissions on the folder
Set-PnPListItemPermission -List $listname -Identity $listItem -User $emailaddress -AddRole $access -ClearExisting
Set-PnPListItemPermission -List $listname -Identity $listItem -User $Staff -AddRole $access

}

#----------END OF SCRIPT DO NOT MODIFY----------#

  • Thanks 1
  • 2 weeks later...
Posted

Ok heres an update , As had to expand on the old script.

 

Here is the Script to Create the CSV of all users in the past 2 days

 


Import-Module ActiveDirectory
$when = ((Get-Date).AddDays(-2)).Date
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "1*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\1xxx.csv" -nti
####Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "2*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\2xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "3*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\3xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "4*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\4xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "5*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\5xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "6*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\6xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "7*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\7xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "8*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\8xxx.csv" -nti
###Next###
Get-ADuser -Filter {whenCreated -ge $when} -SearchBase "OU=KIDS,DC=DOMAIN,DC=org,DC=uk" -Properties DisplayName, PasswordNeverExpires, WhenCreated, UserPrincipalName, sAMAccountName |
where {$_.enabled -eq "true" -and $_.sAMAccountName -like "9*"} |
Select sAMAccountName,UserPrincipalName | Export-csv "C:\Scripts\Sharepoint\FolderCreationKids\CSV\9xxx.csv" -nti
###Next###

Posted

Here is the next part , Puts them in Subfolders so the directory listing isnt too long . I.e. if their username is 1001 it puts them in a folder called 1xxx , creates the account folder , assigns permissions ( including adding staff ) and creates Subject Subfolders.

 

see attached as the script is too long.

 

Part2.txt

 

Have fun ! and i hope i can help some one out .

  • 4 months later...
Posted

Just wanted to chip in on this as it has been very useful for me, I wanted to put a few things down I had to do to get this to work.

 

1) Make sure you are on Powershell 7.2 or greater, I used version 7.4 which can be found here

2) Import the PnP Powershell module - Import-Module PnP.PowerShell

3) As of September 9th 2024 you have to register your own Entra ID Application to use with PnP Powershell, this means in Part 1 of the script

connect-PnPOnline –Url $siteurl -Interactive
will throw an error. This is easy enough to fix and a very helpful guide can be found here

4) You will need to change

connect-PnPOnline –Url $siteurl -Interactive
to
Connect-PnPOnline [yourtenant].sharepoint.com -Interactive -ClientId

5)Remember to change the variables at the beginning of the script to your own paths etc!

6)Lastly, the csv needs column headers of "name" and "EmailAddress", it should look something like this Capture.PNG

 

Here's the redacted version of my Part 1 script for reference:

 

#1
$siteurl = "https://bilbobaggins.sharepoint.com/sites/KS5IndependentTestTeam"                
#2
$listname = "Shared Documents/General"          
#3
$access = "full control"
#4
$Folders = Import-Csv "E:\SubFoldersPermtest.csv"    
#6
$FullPath = "/sites/KS5IndependentTestTeam/Shared Documents/General"
#7
$Staff = "[email protected]"


#----------START OF SCRIPT DO NOT MODIFY----------#
Connect-PnPOnline -Url bilbobaggins.sharepoint.com/sites/KS5IndependentTestTeam -Interactive -ClientId xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx
#–Credentials (Get-Credential)
foreach ( $entry in $Folders )
{

$foldername = $entry.name
$emailaddress = $entry.EmailAddress


#Create Folder
Add-PnPFolder -Name $foldername -Folder $listname 

#get folder properites from list
$listItem = Get-PnPListItem -List $listname -FolderServerRelativeUrl $FullPath -Query "$($foldername)"

Write-Host "setting folder up for $foldername"

#set permissions on the folder
Set-PnPListItemPermission -List $listname -Identity $listItem -User $emailaddress -AddRole $access -ClearExisting
Set-PnPListItemPermission -List $listname -Identity $listItem -User $Staff -AddRole $access

}

#----------END OF SCRIPT DO NOT MODIFY----------#

 

 

Thanks to @2097 for creating and posting the script!

  • Thanks 1
Posted

Oh thanks for the tip about the new requirement to have and Application registration.

 

 

If you are using SDS and Teams you can set an assignment in the class team to do exactly this. The folders are put in the Student Work document library.

Posted

Didnt realise they had made it an requirement for sharepoint ! Looks like ill have to adjust my scripts .

 

Glad it helps people out , Getting staff/kids to use it is another story !

  • Thanks 1
  • 10 months later...
Posted

This script is brilliant, however I'm hoping someone can help.

 

I've adjusted it to suit our tenant & I'm able to generate folders, however it can't seem to set permissions. I'm getting these errors on the permissions section.

 

Line |
  29 |  $listItem = Get-PnPListItem -List $listname -FolderServerRelativeUrl  …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot complete this action.

Please try again.
setting folder up for test teacher
Set-PnPListItemPermission:
Line |
  34 |  Set-PnPListItemPermission -List $listname -Identity $listItem -User $ …
     |                                                      ~~~~~~~~~
     | Cannot bind argument to parameter 'Identity' because it is null.
Set-PnPListItemPermission:
Line |
  35 |  Set-PnPListItemPermission -List $listname -Identity $listItem -User $ …
     |                                                      ~~~~~~~~~
     | Cannot bind argument to parameter 'Identity' because it is null.

 

  • 3 weeks later...
Posted
On 18/09/2025 at 12:46, Olliedawg said:

This script is brilliant, however I'm hoping someone can help.

 

I've adjusted it to suit our tenant & I'm able to generate folders, however it can't seem to set permissions. I'm getting these errors on the permissions section.

 

Line |
  29 |  $listItem = Get-PnPListItem -List $listname -FolderServerRelativeUrl  …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot complete this action.

Please try again.
setting folder up for test teacher
Set-PnPListItemPermission:
Line |
  34 |  Set-PnPListItemPermission -List $listname -Identity $listItem -User $ …
     |                                                      ~~~~~~~~~
     | Cannot bind argument to parameter 'Identity' because it is null.
Set-PnPListItemPermission:
Line |
  35 |  Set-PnPListItemPermission -List $listname -Identity $listItem -User $ …
     |                                                      ~~~~~~~~~
     | Cannot bind argument to parameter 'Identity' because it is null.

 

 

Hi , I believe this could be an issue if you havent clicked on "Files" section in Teams , I dont think teams created the folder structure before you click on it .

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...