So I have a Library with a list of documents. These docments (PDFs) are named in the following format.

  1. DeptCode_MgrNumber_MgrLast,MgrFirst_EmpName_634242

I would like to find a way to

  1. Build a folder for each manager.
  2. Break inheritance for each folder so only the manager can view the contents.
  3. Merge the appropriate documents into the correct folder ( eg managers will have multiple files with their name on it.

I have a great script that builds the file structure (steps 1 and 2) and strips the permissions of each folder. As follows.

#Connect to this Site.
Connect-PnPOnline -Url "https://tenantname.sharepoint.com/sites/sitename" –Credentials (Get-Credential)

#Get the group that contains the accounts of the Managers
$group = Get-PnPGroup "VisitorGroupName of Managers"
#Loop through the users
foreach ($user in $group.Users) {

    #Check to see if folder exist and create it if it does not.
    Resolve-PnPFolder -SiteRelativePath "LibraryName/$($user.Title)"

    #Get the new folder and allfields.
    $folder = Get-PnPFolder -Url "LibraryName/$($user.Title)" -Includes ListItemAllFields

    #Remove all permission and add the Read Role to the user.
    Set-PnPListItemPermission -User $user.Title -AddRole 'Read' -List "LibraryName" -Identity $folder.ListItemAllFields.Id -ClearExisting

    #Remove the Full Control Permission for the account that is running the script.
    Set-PnPListItemPermission -User <Account running the script> -RemoveRole 
    'Full Control' -List "LibraryName" -Identity $folder.ListItemAllFields.Id 


For Item 3 I am at a complete loss I would like to find a way to parse the "MgrNumber" on the title of each file and move it to the corresponding folder.

I have not found much about RegEx in the SharePoint PNP comandlets.
If I have to run a separate script I am fine with that too.

Thank you


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.