locked
Moving files between folders in the same Document library - powershell RRS feed

  • Question

  • Hi,
    is there a simple command which move file between folders in the same Document Library with keeping all metadata fields ?

    Example:

    Entry:

    Site/DocumentLibrary/Folder1/file.docx

    Site/DocumentLibrary/Folder2/

    Result:

    Site/DocumentLibrary/Folder1/

    Site/DocumentLibrary/Folder2/file.docx


    Tuesday, January 17, 2017 12:51 AM

Answers

  • Hi Marek_Sk,

    There is a demo to move files in one folder to another folder in the same library. In my environment, I move files from http://sp:32076/doclib25/aa to http://sp:32076/doclib25/bb, you can change to your site, library and folder.

    $web=Get-SPWeb "http://sp:32076/"
    
    $listName="doclib25"
    
    $list = $web.Lists[$listName]
    
    $TargetFolder = $Web.Url + "/" + $List.RootFolder.Url + "/"+ "bb"
    
    $sourcefolder=$list.rootfolder.SubFolders["aa"]
    
    $files = $sourcefolder.Files
    
    foreach ($item in $files) {
    
         $File=$Web.GetFile($Item.URL);
    
         $TargetPath=$TargetFolder+"/"+$file.name;
    
         $File.MoveTo($TargetPath);
    
         Write-Host "Moved Item:"$item.Name;
    
    }
    
    Write-host "Total Items Moved:"$files.count
    

    Best regards,

    Sara Fan


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Victoria Xia Wednesday, January 25, 2017 1:07 AM
    • Marked as answer by Marek_Sk Monday, February 6, 2017 11:35 PM
    Friday, January 20, 2017 9:17 AM

All replies