Thursday, February 5, 2015

Powershell- Create folders and sub-folders in sharepoint library


The below script creates multiple folders within a document library; Each folder will have one single subfolder within it.



$webUrl = "http://<siteURL>"
$listName = "Library_Name"
$numberFoldersToCreate = 5;
$folderNamePrefix = "folder";


$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

 $StartDateFolder = get-date

for($i=1; $i -le $numberFoldersToCreate; $i++)
{ 

 $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$folderNamePrefix$i")
 $folder.Update()

 Write-Output "Folder created " $folder.Url

# // Creating One subfolder within the folder

 $subFolderURL=$list.ParentWebUrl +"/" + $folder.Url;

#// if your site is a root site collection, then $list.ParentWebUrl will return "/" 
#so use the  line as  $subFolderURL=$list.ParentWebUrl  + $folder.Url; 

 $folder2 = $list.AddItem($subFolderURL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Subfolder")
 $folder2.Update()

 Write-Output "Sub Folder created = " $folder2.Url
  
  
}
$EndDatefolder=get-date

 $foldercreationtime=NEW-TIMESPAN –Start $StartDateFolder  –End $EndDatefolder

 $f=[string]::Concat("Folder creation time = " ,  $foldercreationtime.Hours , ":" , $foldercreationtime.Minutes,  ":" , $foldercreationtime.Seconds   )

Write-Output   $f

$web.Dispose()

No comments:

Post a Comment