Bulk create multiple folders in OneDrive accounts using powershell via csv

AB123 356 Reputation points
2022-09-06T10:46:46.597+00:00

Hi all,

I have 100 users I need to create folders in their onedrive not all users in our tenant.

I have found this which shows how to do this in all users onedrives but if there was away to make it look at a CSV which includs only the 100 users that would be great! - https://learn.microsoft.com/en-us/answers/questions/853168/bulk-create-folder-in-onedrive-accounts-using-powe.html

Please help!

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | OneDrive | For business | Windows
{count} votes

Accepted answer
  1. Jinwei Li-MSFT 4,736 Reputation points Microsoft External Staff
    2022-09-14T09:17:28.85+00:00

    Hi @AB123 ,

    I have found this which shows how to read from a CSV file and create SharePoint groups and add members for your reference.

    After this, you could take folder names from a CSV file and creates folders.

    Like this:
    240945-image.png

    Please try to use this code:

    #Config Variables  
    $SiteURL = "https://crescent.sharepoint.com/sites/Ops"  
    $CSVFilePath = "C:\Temp\Folders.csv"  
    $LibraryName = "Documents"  
       
    Try {  
        #Connect to PnP Online  
        Connect-PnPOnline -Url $SiteURL -Interactive  
        $Web = Get-PnPWeb  
       
        #Get the Document Library and its site relative URL  
        $Library = Get-PnPList -Identity $LibraryName -Includes RootFolder  
        If($Web.ServerRelativeUrl -eq "/")  
        {  
            $LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl  
        }  
        else  
        {  
            $LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl.Replace($Web.ServerRelativeUrl,'')  
        }  
       
        #Get the CSV file  
        $CSVFile = Import-Csv $CSVFilePath  
        
        #Read CSV file and create folders  
        ForEach($Row in $CSVFile)  
        {  
            #Replace Invalid Characters from Folder Name, If any  
            $FolderName = $Row.FolderName  
            $FolderName = [RegEx]::Replace($FolderName, "[{0}]" -f ([RegEx]::Escape([String]'\"*:<>?/\|')), '_')  
       
            #Frame the Folder Name  
            $FolderURL = $LibrarySiteRelativeURL+"/"+$FolderName  
       
            #Create Folder if it doesn't exist  
            Resolve-PnPFolder -SiteRelativePath $FolderURL | Out-Null  
            Write-host "Ensured Folder:"$FolderName -f Green  
        }  
    }  
    catch {  
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
    }  
    

    Reference:
    PowerShell to Bulk Create Folders in a Document Library from a CSV File

    Microsoft provides third-party contact information to help you find additional information about this topic. This contact information may change without notice. Microsoft does not guarantee the accuracy of third-party contact information.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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 additional answers

Sort by: Most helpful

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.