OneDrive for Business: Create a Folder using PowerShell

Requirement: Create a folder in OneDrive for Business site.

How to create a Folder in OneDrive for Business?

OneDrive is a cloud-based storage service provided by Microsoft that allows individuals and teams to store, share and collaborate on files from anywhere. If you are using OneDrive for Business, you may want to create a separate folder to keep your files organized and easy to find. This article will show you how to create a new folder in OneDrive for Business.

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

  1. Login to your OneDrive site (https://<tenant>-my.sharepoint.com). You can open https://www.office.com/, Login with your credentials, and Click on “OneDrive” from the app launcher waffle in the top-left corner of the screen.
  2. Navigate to the location where you want to create the new folder >> From the Toolbar, click on “New”, and choose “Folder”.
  3. Provide a name for your folder and click on the “Create” button to create a folder in OneDrive for Business site.
    onedrive for business powershell create folder

Alternatively, you can create a new folder from OneDrive in File explorer on your PC. Just navigate to the OneDrive folder in Windows Explorer >> Right-click on the white space and choose “New 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
}

Please note that you will need to have Permission on the OneDrive site to run these commands. Also, make sure you have the SharePoint Online PowerShell module (or CSOM SDK) installed.

PnP PowerShell to Create Folder in OneDrive for Business

To create a folder in the user’s OneDrive, we can use the 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 {
    #powershell connect to onedrive for business
    Connect-PnPOnline -Url $OneDriveSiteURL -Interactive
     
    #ensure folder in SharePoint Online using powershell
    Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

We can also use the Add-PnPFolder cmdlet to create a folder in OneDrive for Business using PowerShell.

Ensure a Folder on All OneDrive Sites using PowerShell

Make sure you have Admin rights on all OneDrive sites before running this PowerShell script. Otherwise, you may get “Error: Access denied.”

#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$FolderName = "Archives"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $AdminCenterURL -Credential $Cred

    #Get All OneDrive sites
    $OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"

    #Iterate through Each OneDrive
    ForEach($Site in $OneDriveSites)
    {   
        Try {
            Write-host -f Yellow "Ensuring Folder '$FolderName' in $($Site.URL)" -NoNewline
            #Connect to OneDrive site
            Connect-PnPOnline -Url $Site.URL -Credential $Cred -ErrorAction Stop

            #ensure folder in SharePoint Online using powershell
            $NewFolder = Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName" -ErrorAction Stop

            Write-host -f Green " Done!"
        }
        Catch {
            write-host "`tError: $($_.Exception.Message)" -foregroundcolor Red
        }
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Conclusion

In conclusion, using PowerShell to create a folder in OneDrive for Business can be a useful tool for automating the process and streamlining. The script provided in this guide will allow you to create a new folder in any OneDrive for Business account, either on the root folder or on a specific path within your OneDrive. By following the steps outlined in this guide, you will be able to create new folders in OneDrive for Business efficiently and easily.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “OneDrive for Business: Create a Folder using PowerShell

  • This worked well for me. I only had to enter my credentials once. My script is modified to read OneDrive accounts from a csv file. $OneDriveSites = Import-Csv -Path “$env:OneDrive\SPO-Scripts\OneDriveNames.csv”

    Reply
  • What would be the method/syntax, if you wanted to create a folder on ALL users’ OneDrive?

    Reply
    • Post has been updated with a PowerShell script to ensure a folder on All OneDrive sites.

      Reply
  • 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

Your email address will not be published. Required fields are marked *