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?
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:
- 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 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 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 {
#Connect to PnP Online
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
$SiteConn = Connect-PnPOnline -Url $Site.URL -Credential $Cred -ReturnConnection -ErrorAction Stop
#ensure folder in SharePoint Online using powershell
$NewFolder = Resolve-PnPFolder -SiteRelativePath "Documents/$FolderName" -Connection $SiteConn -ErrorAction Stop
Disconnect-PnPOnline -Connection $SiteConn
Write-host -f Green " Done!"
}
Catch {
write-host "`tError: $($_.Exception.Message)" -foregroundcolor Red
}
}
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
What would be the method/syntax, if you wanted to create a folder on ALL users’ OneDrive?
Post has been updated with a PowerShell script to ensure a folder on All OneDrive sites.
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
Sure, You can use this PowerShell script: How to Copy a File in SharePoint Online using PowerShell?