SharePoint 2013: PowerShell to Create a Folder in Document Library

Requirement: Create a new folder in SharePoint

How to add folder in SharePoint Document Library using PowerShell:
To create folder in document library in SharePoint 2013, use this PowerShell script:
$folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "New")
The above codes doesn't provide much fault tolerance, isn't it?
  • What if the particular library we are looking for doesn't exists?
  • What if the folder we are trying to add already exists on the library?
Error! So lets re-write the code in PowerShell with little fault tolerance!
create folder in sharepoint document library powershell

How to Create Folders and Sub-Folders in SharePoint List using PowerShell?
Here is the PowerShell code snippet for creating Folders and Sub-Folders inside SharePoint List or Library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web=Get-SPWeb ""
#Get the List/Library

If($list)  #Check If List exists!
    #Check Folder Doesn't exists in the Library!
    $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Sales List");
    #sharepoint powershell check if folder exists
    If($folder.Exists -eq $false)
       #Create a Folder
       $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Sales Sub-Item")
    #Create a Sub-Folder "APAC Sales Documents" inside "Sales Documents"    
    $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "APAC Sales Documents");
    #Check if sub-folder doesn't exists already
    If ($Subfolder.Exists -eq $false)
       #Create a Sub-Folder Inside "Sales Documents"
       $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "APAC Sales Documents")

Tags: sharepoint powershell folders add, sharepoint 2013 powershell add folder to document library, sharepoint 2013 powershell add folder, sharepoint powershell create folder,  sharepoint powershell create subfolder, sharepoint powershell create folder in document library, sharepoint create folder in list powershell, sharepoint powershell new folder


  1. saved ton of time . Thank you :-)

  2. Error when attempting to create the folder:"Invalid Yes/No value.
    A Yes/No field contains invalid data. It should be either 1 or 0. Please check the value and try again."

    Could you please help me?

  3. how to pass the folder name in a variable


Please Login and comment to get your questions answered!

Powered by Blogger.