creating folders in specific users onedrives

AB123 356 Reputation points
2023-08-03T10:15:45.1266667+00:00

I want to create specific folders like 'ICT' and 'RE' in multiple users' OneDrive accounts, but only for a select group of users. Is it possible to achieve this using PowerShell?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,906 questions
{count} votes

Accepted answer
  1. Zehui Yao_MSFT 5,876 Reputation points
    2023-08-14T10:16:55.79+00:00

    Hi AB123, per my knowledge, currently, you can only create folders for one user in OneDrive with the following script.

    #Load SharePoint Online CSOM Assemblies
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
     
    #Parameters
    $OneDriveSiteURL = "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"
    $FolderName = "Archives"
     
    #Setup Credentials to connect
    $Cred = Get-Credential
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($OneDriveSiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
     
        #Get the default "Documents" Library
        $List = $Ctx.Web.Lists.GetByTitle("Documents")
      
        #Check if Folder Exists already
        $Folders = $List.RootFolder.Folders
        $Ctx.Load($Folders)
        $Ctx.ExecuteQuery()
      
        #Get existing folder names
        $FolderNames = $Folders | Select -ExpandProperty Name
        if($FolderNames -contains $FolderName)
        {
            write-host "Folder Exists Already!" -ForegroundColor Yellow
        }
        else
        {
            #onedrive for business powershell create folder
            $NewFolder = $List.RootFolder.Folders.Add($FolderName)
            $Ctx.ExecuteQuery()
            Write-host "Folder '$FolderName' Created Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
    

    Hope this helps.

    Best Regards.


    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.