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 "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 exists 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.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.