SharePoint Online: Create a Folder using PowerShell

Requirement: Create a Folder in SharePoint Online Document Library.

Folders are used to organize files in SharePoint, similar to what we do on our computers. When it comes to managing files, creating folders in SharePoint Online is the most efficient option and makes it easier to find what you’re looking for. A typical SharePoint folder is a container for all the files and sub-folders you create in your document library. There are many ways to create a new folder in SharePoint Online. Let’s see the most common methods used when creating folders on the SharePoint Online site. You can add a folder to the SharePoint Online list or library by following the below steps:

How to Create a Folder in SharePoint?

Folders can be created in any list or library in SharePoint Online where the “New Folder” feature is turned ON. By default, folders are enabled in libraries, whereas in lists, they are disabled. Like folders in Windows Explorer, SharePoint Folders are used to organize files into a structure. So, how to create a folder in SharePoint? Creating a folder in a SharePoint library is simple and straightforward.

To create a folder in SharePoint Online, follow these steps:

  1. Login to your classic SharePoint Online site, and navigate to your document library where you want to create a folder in the web browser.
  2. On the ribbon menu bar, click on the Files tab (Items tab, if it is a list, instead of a library).
  3. Click on the New Folder icon.
    create new folder sharepoint online
  4. In the create a new folder dialog, Enter the folder name and click Save!
    sharepoint online create folder powershell

Now you can see your new folder in the Document Library.

Add New Folder in Modern SharePoint Libraries

Similar to the classic experience, you can create a new folder in a few clicks.

  1. Navigate to the SharePoint site and the library where you want to create a new folder.
  2. Access the library by selecting its name on the Quick Launch bar, or by clicking on Settings, choosing Site contents, and then clicking on the name of the library where folders will be added.
  3. click on the New menu of the toolbar and then “Folder”. Type in the new folder name and click the create button.
    create folder in sharepoint online using powershell

Your folder has been created! Once you have created the folder, you can upload files to the selected folder.

Folder or Metadata? Which one should you choose?

The answer is both! A SharePoint folder is a container for all the files and sub-folders that you create in your document library. This makes it easier to store all your document files and sub-folders. Metadata is information about the file itself, such as its name, size, and last modified date. You can add metadata capabilities to any file by clicking on the Details tab and entering information into the fields. So, in conclusion, a combination of views and folders might work best!

Please note, creating a folder is less preferred compared with adding a meta-data column to classify data in SharePoint!

Permissions to create a Folder in SharePoint Online library: You need at least “Contribute” Permissions on the document library in which you want to create a folder! The above steps are applicable to the SharePoint server version of SharePoint, too!

How to enable the “New Folder” option?

By default, Folder creation is enabled on the SharePoint Online document libraries. What if the New Folder option is grayed out? If the New Folder button isn’t available, here is how you can enable the New Folder command:

  1. Login to your SharePoint site >> Navigate to the List or Library settings >> click Advanced settings.
  2. Click the “Yes” option in the Folder section to make the “New Folder” menu item available.
  3. Click OK to save your changes.

Now, you should get the “New folder” in the SharePoint list. Let’s see how to create folders in SharePoint Online with PowerShell.

How to add a SharePoint folder to file explorer?
You can also create a folder in Explorer View! Just open the library in File Explorer and create a new folder by simply right click >> New >> Folder

SharePoint Online: Create a Folder using PowerShell

Instead of manually creating folders in your SharePoint Online document library, You can use PowerShell to do this in a few lines of script. Let us create a folder in the SharePoint document library using PowerShell CSOM:

#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListURL="/sites/marketing/Shared Documents"
$FolderName="Reports"
$UserName="salaudeen@crescent.com"
$Password ="password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

Try { 
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials
  
    #Get the List Root Folder
    $ParentFolder=$Context.web.GetFolderByServerRelativeUrl($ListURL)

    #sharepoint online powershell create folder
    $Folder = $ParentFolder.Folders.Add($FolderName)
    $ParentFolder.Context.ExecuteQuery()

    Write-host "New Folder Created Successfully!" -ForegroundColor Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 

Create a Sub-Folder at the given path using PowerShell:

We can add a folder or sub-folder to any existing library or folder using PowerShell.

    
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $credentials

#sharepoint online powershell create folder in document library
$Folder=$Context.Web.Folders.Add("Shared Documents/Reports/V2")
$Context.ExecuteQuery()

Write-host "Folder Created at: " $Folder.ServerRelativeUrl -ForegroundColor Green
How many folders can you have in SharePoint Online? Technically, up to 30 million files and folders! More info: SharePoint Limits

SharePoint Online: PowerShell to create a folder in the document library

Let us wrap it inside a function and create a folder in SharePoint Online using PowerShell.

#Load SharePoint 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"
 
Function Create-Folder()
{
    param(
        [Parameter(Mandatory=$true)][string]$SiteURL,
        [Parameter(Mandatory=$false)][System.Management.Automation.PSCredential] $Cred,
        [Parameter(Mandatory=$true)][string]$LibraryName,
        [Parameter(Mandatory=$true)][string]$FolderName
    )

    Try {
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Library by Name
        $List = $Ctx.Web.Lists.GetByTitle($LibraryName)

        #Check 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 #powershell sharepoint online create folder if not exist
        {
            #sharepoint online create folder powershell
            $NewFolder = $List.RootFolder.Folders.Add($FolderName)
            $Ctx.ExecuteQuery()
            Write-host "Folder '$FolderName' Created Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Creating Folder!" $_.Exception.Message
    }
}

#Call the function to delete list view
Create-Folder -SiteURL "https://crescent.sharepoint.com" -Cred (Get-Credential) -LibraryName "Project Documents" -FolderName "Active"

This PowerShell creates the directory if it does not exist. Let’s see how to create a SharePoint folder using PnP PowerShell.

Create a Folder in SharePoint Online using PnP PowerShell

Use the Add-PnPFolder cmdlet to add a folder in SharePoint Online. Here is the SharePoint Online PowerShell to create a folder in a document library:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderName= "Team Projects"
$SiteRelativeURL= "/Shared Documents" #Site Relative URL of the Parent Folder

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #sharepoint online create folder powershell
    Add-PnPFolder -Name $FolderName -Folder $SiteRelativeURL -ErrorAction Stop
    Write-host -f Green "New Folder '$FolderName' Added!"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

What if the folder exists already? Here is how to create a folder in SharePoint Online so that you can better organize your data. The Resolve-PnPFolder cmdlet checks if the folder exists. If not, it creates a new one with the following command:

#Set Parameters
$WebURL = "https://crescent.sharepoint.com/sites/marketing"
$FolderURL = "Project Documents/2018"

#Connect to PnP Online
Connect-PnPOnline -Url $WebURL -Credentials (Get-Credential)

#Create Folder if it doesn't exist
Resolve-PnPFolder -SiteRelativePath $FolderURL

Bulk Add Multiple Folders from A-Z

Want to create folders A to Z in a SharePoint Online document library or folder? Here is the PowerShell script to make multiple folders in one go:

#Config Variables
$SiteURL="https://crescent.sharepoint.com/sites/Retail"
$FolderSiteRelativeURL = "Invoices V2"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #SharePoint Bulk Create Folder
    $Array = 65..90
    ($Array).ForEach({
        $Char = [char]$_
        Add-PnPFolder -Name $Char -Folder $FolderSiteRelativeURL
    })
 
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This script creates folders from A to Z in the given document library:

create multiple folders in sharepoint online using PowerShell

What if you want to create Folders for each Month? From January to December:

#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Retail"
$FolderSiteRelativeURL = "Shared Documents"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Create Folder for Each Month
    $Array = 1..12
    ($Array).ForEach({
        $FolderName = (Get-Culture).DateTimeFormat.GetMonthName($_)
        Add-PnPFolder -Name $FolderName -Folder $FolderSiteRelativeURL
    }) 
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

What are some best practices for naming folders in SharePoint?

  • Use clear and concise names that accurately reflect the folder’s content.
  • Avoid using special characters or symbols that might cause compatibility issues.
  • Maintain consistency in your naming conventions for better organization.
  • Consider using prefixes or suffixes to categorize folders based on project, department, or other criteria.
  • Limit the depth of nested folders to avoid complexity.
  • Consider the use of metadata and views to organize and find content instead of relying solely on folders.

Conclusion

Creating folders in SharePoint Online is a simple process that allows you to organize files and content efficiently in document management systems. Developing a folder hierarchy and using consistent metadata also makes it easier to locate files and information later. By following this step-by-step guide, you can quickly and easily create folders, organize your documents, and enhance collaboration within your organization. Making use of folders to streamline business processes is an easy way to get more value from your SharePoint Online platform.

How do I create a bulk folder in SharePoint Online? If you want to create multiple folders in bulk from a CSV file, use: SharePoint Online: PowerShell to Create Multiple Folders in a Document Library from a CSV

Can you make folders private in SharePoint Online?

Yes! You can edit the Folder permissions and share it privately with specific users in SharePoint. You can also define their permission levels (e.g., view only, edit, or full control) to manage access to the folder’s content.
More info: Restrict access to a folder in SharePoint Online

How do I upload a folder to SharePoint Online?

You can upload a folder with all its files and subfolders by dragging and dropping it into a SharePoint Online document library. File Explorer view and PowerShell methods can also help upload a folder and its contents.
More info: Upload folder to SharePoint Online using PowerShell

How do I find the URL of a SharePoint folder?

To get the direct URL of a folder in SharePoint: Navigate to the document library where the folder is located >> Select the folder >> On the information panel, scroll to the bottom. Click on the little copy icon next to “Path”. This gives you the direct link to the folder.

How do I add a link to a SharePoint folder in Explorer?

You can add your frequent SharePoint Online folder to “Quick Access” in File Explorer for easy access.
More info: How do I add a folder to SharePoint in Explorer?

How do I delete a folder from my SharePoint Online?

To delete a folder from your SharePoint Online, go to the list or document library of that Folder. Select the folder you want to delete and click on Delete from the command bar. You can also use PowerShell to remove a folder in SharePoint Online.
More info: Delete a Folder in SharePoint Online

Can I create a folder within another folder in SharePoint?

Yes, you can create a nested folder within another folder in SharePoint. Simply navigate to the parent folder where you want to create the new folder, and follow the same steps as creating a primary folder. This helps in organizing files and documents more effectively.

How can I share a SharePoint folder with external users?

To share a particular folder, navigate to the document library and click on the Share button, then input the external user’s email address you wish to invite people to. If the external sharing feature is turned off, activate it by visiting the SharePoint admin center.

Can I move or copy a folder to another location in SharePoint?

Yes, SharePoint allows you to move or copy folders and their contents to another location within the same site or across sites. Use the “Move to” or “Copy to” options available in the document library menu. This is handy for reorganizing content or duplicating information for different purposes.

I don’t see the “Folder” option in the “+ New” menu. Why?

The “Folder” option might be disabled. Go to the SharePoint document library settings >> Click on “Advanced settings” >> Find the “Folders” section, and you can choose to enable or disable folder creation by selecting the appropriate option. Site owners or users with designer permissions can enable it.

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!

8 thoughts on “SharePoint Online: Create a Folder using PowerShell

  • Anyway to create folders with powershell for sharepoint site pages?

    Reply
    • The “New Folder” option is disabled in the “Site Pages” library by default! You can enable it by going to Library settings >> Advanced Settings >> and set the Make “New Folder” command available? to “Yes”

      Reply
  • Error handling doesn’t work with Add-PnPFolder.

    Add-PnPFolder : A file or folder with the name https://XXXX.sharepoint.com/sites/XXX/SBU DBA/Applied Materials/AMAT Eng Projects T and M already
    exists.
    At line:6 char:8
    + $a=Add-PnPFolder -Name “AMAT Eng Projects T and M” -Folder “SBU D …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : WriteError: (:) [Add-PnPFolder], ServerException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFolder
    New Folder ” Added!

    Reply
  • any idea why created folder wont appear on the Sharepoint list?
    Folder created manually from Ribbon shows correct.
    In debugger i list all folders : created by csom and manualy are listed in array.
    Cant see the difference in those Folder objects.

    Any idea?

    Reply
    • That could be because of your View settings! Edit the List view, under folders, Set the Option to “Show items inside folders” instead of “Show all items without folders”

      Reply
    • I have the same problem like the anonymous

      Reply
    • Even I am facing the same issue, other folders are visible which were created manually

      Reply

Leave a Reply

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