PowerShell SharePoint Online - Importing data from CSV where values have spaces?

frob 4,281 Reputation points
2021-08-20T23:23:42.203+00:00

Hi there

I have a CSV file with lots of SharePoint Online Document Library folder paths along with users and permissions like this:

125174-image.png

I need to load the CSV and run Set-PnPFolderPermission for each row, so the users are granted permissions to the relevant folders in listed SP Doc Libraries.

$Data = Import-CSV "C:\Users\John\Users.csv";  
foreach ($Record in $Data)  
{  
 Set-PnPFolderPermission -List $Record.LibraryName -Identity $Record.Path -User $Record.User -AddRole $Record.Role;  
}  

The command runs if I use it outside the loop on its own. However, it fails inside the loop. Maybe because of the apostrophes and spaces in the Path and the LibraryName fields? Please suggest how to get this to work inside the loop.

Thank you.

Microsoft 365 and Office | SharePoint | Development
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. JoyZ 18,126 Reputation points
    2021-08-23T09:57:44.957+00:00

    @frob ,

    Add double quotes to the relevant attributes to compare the results:

    Set-PnPFolderPermission -List $Record."LibraryName" -Identity $Record."Path" -User $Record.User -AddRole $Record.Role;  
    

    If an 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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 48,026 Reputation points
    2021-08-21T02:02:27.293+00:00

    The identity parameter is relative URL. Shouldn't the backslash be a forward slash?

    There may be other characters that need to be escaped, too. But that's something that's related to SharePoint, not to PowerShell. Adding a tag to your original question so it's brought to the attention of people that deal with that product would probably get you a better answer.


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.