Sunday 24 January 2016

SharePoint 2013 PowerShell script to create sub folder in Document Library.

Note : Tested in SharePoint 2013

Script will get the date from server and create the sub folder in library named as server date.

Below is the script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Get the Web
$web=Get-spweb "Your Site Collection"
#Get the List/Library
$list=$web.Lists.TryGetList("Your Document Library Name")
$RootFolder=$web.GetFolder("Your Root Folder name")

$foldername = Get-Date -Format "yyyy-MM-dd"
 foreach( $folder in $RootFolder.SubFolders )
 {
    if($folder.name -ne "Forms")  
    {
    $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "$foldername");
  #write-host $Subfolder
  if ($Subfolder.Exists -eq $false)
   {
    #Create a Sub-Folder Inside "Sales Documents"
    $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$foldername")
    $Subfolder.Update();
    Write-host "Sub-Folder created successfully"
   }
    else
   {
     write-host "Sub-Folder already exists"
   }
    }


 }


No comments:

Post a Comment