SharePoint Online: Create a Folder using PowerShell

Create a Folder in SharePoint Online Document Library
Folders are used to organize files in SharePoint, similar to what we do on our computers. You can add a folder to SharePoint online list or library by following the below steps.

How to Create a Folder in SharePoint Online?

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. To create a folder in SharePoint Online, follow these steps:

  • Login to your SharePoint online site, Navigate to your document library in which you want to create a folder.
  • On the ribbon, click on Files tab (Items tab, if it is a list, instead of library)
  • Click on “New Folder” button.
    create new folder sharepoint online
  • In the Create A New Folder window, enter the folder name and click Save!
    sharepoint online create folder powershell

Now you can see your new folder in the Document Library. Similarly, in modern experience, you can create a new folder by clicking on the “New” button in the toolbar and then “Folder”.

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

BTW, You need at least “Contribute” Permissions on the document library in which you want to create a folder!

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, you can enable it.

  • Navigate to the List or Library setting >> click Advanced settings.
  • In the Folder section, click the “Yes” option to make the “New Folder” menu item available.
  • Click OK to save your changes.

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

SharePoint Online: Create a Folder using PowerShell

Let us create a folder in the SharePoint document library using PowerShell CSOM.

#Variables for Processing
$SiteUrl = ""
$ListURL="/sites/marketing/Shared Documents"
$UserName="[email protected]"
$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

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

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

Create Sub-Folder at 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")

Write-host "Folder Created at: " $Folder.ServerRelativeUrl -ForegroundColor Green

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()
        [Parameter(Mandatory=$false)][System.Management.Automation.PSCredential] $Cred,

    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

        #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)
            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 "" -Cred (Get-Credential) -LibraryName "Project Documents" -FolderName "Active"

This PowerShell creates the directory if not exists.

Create Folder in SharePoint Online using PnP PowerShell

Use 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 = ""
$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?

#Set Parameters
$SiteURL = ""
$FolderURL = "Project Documents/2018"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

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

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

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • March 24, 2020 at 10:47 AM

    Error handling doesn’t work with Add-PnPFolder.

    Add-PnPFolder : A file or folder with the name DBA/Applied Materials/AMAT Eng Projects T and M already
    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!

  • February 28, 2019 at 12:10 PM

    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?

    • March 1, 2019 at 8:00 AM

      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”

    • July 5, 2019 at 3:10 PM

      I have the same problem like the anonymous

    • July 21, 2019 at 5:55 AM

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


Leave a Reply