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

frob 4,226 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.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,794 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,027 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,546 questions
0 comments No comments
{count} votes

Accepted answer
  1. JoyZ 18,071 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 46,801 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 Answers by the question author, which helps users to know the answer solved the author's problem.