0

Environment: SharePoint 2010 On-premise

My scenario:

One of the users has uploaded 32,000+ folders along with files into one folder in SharePoint document library. few of those newly created 32,000 folders have subfolders and file as well.

I have set SharePoint threshold to 15,000 and I don't want to increase it as we know that increase threshold limit from certain level can cause some unexpected issue especially performance degrades.

What I want is to : segregate those folders (along with subfolders and associate files with metadata) by year and move those folders into specific year folder.

What I have done so far : I have created year wise folder named 2011, 2012 to 2017. Thankfully those newly uploaded folder has a relevant naming convention like those folders name starts with 2011-, 2012- to 2017-.

I have created a Power shell script by providing folder name as a parameter and loop through recursively to each folder. I have put a condition in power shell with a switch so I can bifurcate those folders by year and fetch the file.

as I have mentioned, I have created year wise folder and have bifurcate those newly uploaded folder using switch with name , So I have source folders and I have destination folders and I can create the new folder and move associate file and after moving I can delete the source folder so as the count reduce without increasing threshold , users can access things.

The problem I am facing and looking for a solution :

  1. How to move folder instead of creating the new folder and if I have to create a new folder then How I can preserve metadata specially created, modified, created by and modified by data of folder while creating a new. Thankfully this folder hierarchy's permission is inherited so not to worry about permission preservation.

  2. I can create a new folder into a destination from the source and move the files, How I can also move subfolders and associated files. (as with recursive my script don't able to remember parent folder and it's just looping through and based on condition it creates a folder into year folder (if name match like 2011-)) otherwise, it's not checking. So How I move subfolder of source folder into exactly destination folder (same hierarchy).

Open with explorer is also not working for this path as the limit is increase and I don't want to increase the limit in the registry. I know the approach to open with explorer and cut paste those folders into year wise and run the script to take ownership of those documents and published it. But Don't want to mess up with Registry.

The script I have created till now.

# Add SharePoint Snapin if not using SharePoint's PowerShell Console
#----------------------------------------------------------------------

if((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Get folders recursively
#---------------------------------
function GetFolder($fldr){

#Loop through all the sub folders
#-----------------------------------------
foreach($subfoldername in $fldr.SubFolders){

$site="http://SiteName"
$web=Get-SPWeb -identity $site
$list=$web.Lists.TryGetList("DocumentLibraryName")

# Call to Function to move Data.
#-------------------------------------------

# Recursive function
#---------------------------------------
GetFolder $subfoldername

}

}

#This function will move folders & files into a different folder
function Movefolderandfile{

param(
$spsite,
$currFolder
)

$spSite="http://SiteName"
$web=Get-SPWeb -Identity $spSite
$list=$web.Lists.TryGetList("Document Library Name")
[Microsoft.SharePoint.SPFolder] $subFolderName=$web.GetFolder($site + $currFolder)

# Get Path of the Year 2011 to 2017

$sp2011path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2011"
$sp2012path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2012"
$sp2013path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2013"
$sp2014path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2014"
$sp2015path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2015"
$sp2016path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2016"
$sp2017path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2017"

# Get Folder from path

$sp2011folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2011path)
$sp2012folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2012path)
$sp2013folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2013path)
$sp2014folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2014path)
$sp2015folder=$web.GetFolder($list.rootfolder.url + "/"+$sp2015path)
$sp2016folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2016path)
$sp2017folder=$web.GetFolder($list.rootfolder.url +"/"+$sp2017path)


# Move Folder and file into Year 2011
    if($currFolder.Name).contains("2011-")
    {
        **# Get folder name and create under year folder. I want to move with metadata not create new folder please suggest and How I can move to associate subfolder instead of specifying year folder**

        # Move files from the source folder to  $sp2011folder, 

        # Delete the source folder

    }
    elseif($currFolder.Name).contains("2012-")
    {
        # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2012folder

        # Delete the source folder
    }
    elseif($currFolder.Name).contains("2013-")
    {
        # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2013folder

        # Delete the source folder
    }
    elseif($currFolder.Name).contains("2014-")
    {
        # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2014folder

        # Delete the source folder
    }
    elseif($currFolder.Name).contains("2015-")
    {
        # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2015folder

        # Delete the source folder
    }
    elseif($currFolder.Name).contains("2016-")
    {
        # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2016folder

        # Delete the source folder
    }
    elseif($currFolder.Name).contains("2017-")
    {
        # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2017folder

        # Delete the source folder
    }
    elseif($currFolder.Name).contains("2018-")
    {
       # Get folder name and create under year folder.

        # Move files from the source folder to  $sp2018folder

        # Delete the source folder
    }
}



#Main Function
#----------------------------------

function move-folder-with-metadata-into-anotherfolder{
param(
$site,
$library,
$folder
)

# Find Web object
#-----------------------
$web=Get-SPWeb -Identity $site
$spLibrary=$spSite.Folders[$library]
# Find Document Library object
#---------------------------------------
$list=$web.Lists.TryGetList("Document Library Name")

# Find Folder Object
#-----------------------------------------
[Microsoft.SharePoint.SPFolder] $spfolder=$web.GetFolder($list.rootFolder.URL+"/"+$folder)

Get-Date





}

My function will run with

function move-folder-with-metadata-into-anotherfolder (by passing $site, $library, $folder)

I have read somewhere that we can also use SharePoint designer workflow for the same I am fine with that if the same is helpful for this requirement.

No CSOM as I don't want to deploy or run any EXE on the server, I want to stick with Power shell if it's programming

Please let me know if you need any clarification on problem, Looking forward to solve this together.

1

2 Answers 2

1

Moving whole folder including subfolders and files is simple. Find the item (folder) and call MoveTo method on it.

$web = Get-SPWeb https://sharepoint.domain.com/sites/somesite

# Grab the folder by server-relative url
$folder = $web.GetFileOrFolderObject("/sites/somesite/DocumentLibrary/2017")

# Destination folder must not exist because MoveTo will create it
$folder.MoveTo("/sites/somesite/DocumentLibrary/Some subfolder/2017")
$web.Dispose()

Reference:
https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ms461652(v%3Doffice.15)
https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms439829(v%3Doffice.14)

2
  • Many Thanks, Lukas, I will check and let you know the outcome. Commented Jan 4, 2019 at 6:16
  • Thank you Lukas, It's work. Just one thing to inform readers that we need to create automatic path for $folder.Moveto() Example: $newfolder=$web.GetFolder($sp2011folder.url +"/"+$subFolderName.Name) if($newfolder.Exists -eq $false){ $subFolderName.MoveTo($newfolder) } Commented Jan 4, 2019 at 6:54
0
# Add SharePoint Snapin if not using SharePoint's PowerShell Console
#----------------------------------------------------------------------

if((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Get folders recursively
#---------------------------------
function GetFolder($fldr){

#Loop through all the sub folders
#-----------------------------------------
foreach($subfoldername in $fldr.SubFolders){

$site="http://siteName"
$web=Get-SPWeb -identity $site
$list=$web.Lists.TryGetList("Document library Name")

# Call to Function to move Data.
#-------------------------------------------
Movefolderandfile -spsite $site -currFolder $subfoldername
# Recursive function
#---------------------------------------
GetFolder $subfoldername

}

}

#This function will move folders & files into different folder
function Movefolderandfile{

param(
$spsite,
$currFolder
)

$spSite="http://siteName"
$web=Get-SPWeb -Identity $spSite
$list=$web.Lists.TryGetList("Document Library Name")
[Microsoft.SharePoint.SPFolder] $subFolderName=$web.GetFolder($site + $currFolder)

# Get Path of Year 2011 to 2017

$sp2011path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2011"
$sp2012path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2012"
$sp2013path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2013"
$sp2014path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2014"
$sp2015path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2015"
$sp2016path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2016"
$sp2017path="RootFolder/Subfolder1/Subfolder2/subfolder 3/Subfolder4/2017"

# Get Folder from path

$sp2011folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2011path)
$sp2012folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2012path)
$sp2013folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2013path)
$sp2014folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2014path)
$sp2015folder=$web.GetFolder($list.rootfolder.url + "/"+$sp2015path)
$sp2016folder=$web.GetFolder($list.rootfolder.url+"/"+$sp2016path)
$sp2017folder=$web.GetFolder($list.rootfolder.url +"/"+$sp2017path)

if($list){

    if(($subFolderName.Name).contains("2011-")){
        # Get folder name and create the folder under @sp2011folder.
        Try{ 

         $newfolder=$web.GetFolder($sp2011folder.url +"/"+$subFolderName.Name)
         $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"
         if($newfolder.Exists -eq $false){
            $subFolderName.MoveTo($newfolder)
            $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
            "-----------------------------------------------------------------------------`n"
         }
         }
         Catch{
            $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

         }

    }
    elseif(($subFolderName.Name).contains("2012-")){
        Try{
        # Get folder name and create the folder under @sp2012folder.
         $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder    
         $newfolder=$web.GetFolder($sp2012folder.url +"/"+$subFolderName.Name)
         "-----------------------------------------------------------------------------`n"
         if($newfolder.Exists -eq $false){
         $subFolderName.MoveTo($newfolder)
         $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"
         }
         }
         Catch{
            $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

         }

    }
    elseif(($subFolderName.Name).contains("2013-")){
    Try{
        # Get folder name and create the folder under @sp2013folder.

         $newfolder=$web.GetFolder($sp2013folder.url +"/"+$subFolderName.Name)
         $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder        
         "-----------------------------------------------------------------------------`n"
         if($newfolder.Exists -eq $false){
         $subFolderName.MoveTo($newfolder)
         $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"
         }
       }
    Catch{

          $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

         }

    }
    elseif(($subFolderName.Name).contains("2014-")){
    Try{
        # Get folder name and create the folder under @sp2014folder.

         $newfolder=$web.GetFolder($sp2014folder.url +"/"+$subFolderName.Name)
         $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"        
         if($newfolder.Exists -eq $false){
         $subFolderName.MoveTo($newfolder)
         $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"
         }
       }
       Catch{

            $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

       }


    }
    elseif(($subFolderName.Name).contains("2015-")){
    Try{
        # Get folder name and create the folder under @sp2011folder.

         $newfolder=$web.GetFolder($sp2015folder.url +"/"+$subFolderName.Name)
         $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"        
         if($newfolder.Exists -eq $false){
         $subFolderName.MoveTo($newfolder)
         $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"
         }
       }
       Catch{
            $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

       }

    }
    elseif(($subFolderName.Name).contains("2016-")){
    Try{
        # Get folder name and create the folder under @sp2016folder.

         $newfolder=$web.GetFolder($sp2016folder.url +"/"+$subFolderName.Name)
         $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder        
         "-----------------------------------------------------------------------------`n"
         if($newfolder.Exists -eq $false){
         $subFolderName.MoveTo($newfolder)
         $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
         "-----------------------------------------------------------------------------`n"
         }
       }
     Catch{

        $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

       }


    }
    elseif(($subFolderName.Name).contains("2017-")){
            Try{
                    # Get folder name and create the folder under @sp2017folder.

                    $newfolder=$web.GetFolder($sp2017folder.url +"/"+$subFolderName.Name)
                    $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder
                    "-----------------------------------------------------------------------------`n"        
                    if($newfolder.Exists -eq $false){
                                $subFolderName.MoveTo($newfolder)
                                $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
                                 "-----------------------------------------------------------------------------`n"
                    }

                }
            Catch{
                  $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

                  }

        }
         elseif(($subFolderName.Name).contains("2018-")){
                Try{
                        # Get folder name and create the folder under @sp2018folder.

                        $newfolder=$web.GetFolder($sp2018folder.url +"/"+$subFolderName.Name)
                        $subFolderName.Name+" "+"from"+" "+ $subFolderName.url+" "+ "Will move to"+" "+$newfolder
                        "-----------------------------------------------------------------------------`n"
                        if($newfolder.Exists -eq $false){
                                    $subFolderName.MoveTo($newfolder)
                                    $subFolderName.Name+" "+"has been moved to"+" "+$newfolder
                                    "-----------------------------------------------------------------------------`n"
                                    }
                    }
               Catch{
                         $_ | out-file -filepath "C:\Himanshu\move folders with files into other location in the same-document library\errors.txt" -Append

                    }

         }
    }
}



#Main Function
#----------------------------------

function move-folder-with-metadata-into-anotherfolder{
param(
$site,
$library,
$folder
)

# Find Web object
#-----------------------
$web=Get-SPWeb -Identity $site

# Find Document Library object
#---------------------------------------
$list=$web.Lists.TryGetList("Document Library Name")

# Find Folder Object
#-----------------------------------------
[Microsoft.SharePoint.SPFolder] $spfolder=$web.GetFolder($list.rootFolder.URL+"/"+$folder)

Get-Date

GetFolder $spfolder



}

Run This script with

move-folder-with-metadata-into-anotherfolder -site "http://SiteName" -library "LibraryName" -folder "RootFolder/SubFolder1/SubFolder2">.\checkin.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.