OneDrive for Business: Create Folder using PowerShell

Requirement: Create a folder in OneDrive for Business site.

How to create a Folder in OneDrive for Business?

To create a new folder in OneDrive for Business site, do the following:

  • Login to your OneDrive site (https://<tenant>-my.sharepoint.com)
  • From the Toolbar, click on “New”, choose “Folder”
  • Provide a name to your folder and click on the “Create” button to create a folder in OneDrive for Business site.
    onedrive for business powershell create folder
You must have site collection administrator rights to access other user’s OneDrive! How to Add Site Collection Admin Access to OneDrive for Business Sites in Office 365?

OneDrive for Business: PowerShell to Create Folder

To create a folder in OneDrive for Business site, use this PowerShell script. Set the parameters according to your requirements.

#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
}

PnP PowerShell to Create Folder in OneDrive for Business

To create a folder in user’s OneDrive, we can use Resolve-PnPFolder cmdlet. This cmdlet creates a new folder if it doesn’t exist already.

#Parameters
$OneDriveSiteURL = "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"
$FolderName = "Archives"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $OneDriveSiteURL -UseWebLogin
     
    #ensure folder in SharePoint online using powershell
    Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

We can also use Add-PnPFolder cmdlet.

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

One thought on “OneDrive for Business: Create Folder using PowerShell

  • May 19, 2021 at 2:39 PM

    Hi Sir,

    Is there any script to copy file from one folder to another folder within the same OneDrive for Business?
    like destination file will not be override, we need to copy file on daily basis with different name/date. Could you please help on this.

    Thanks in Advacnce.

    Thanks,
    SV

    Reply

Leave a Reply