You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写PowerShell脚本迁移SharePoint文档库指定文件至冗余文件夹

PowerShell Script to Migrate Specific Files to SharePoint's Redundant Folder

Got it, let's fix this SharePoint file migration script properly. The original approach tries to use local file system commands (like Get-ChildItem and Move-Item) on SharePoint objects, which won't work because SharePoint's file structure isn't the same as your local drive. We need to use SharePoint's Server Object Model to handle this correctly.

Here's the Corrected Script

# Configuration - Update these values to match your environment
$WebURL = "http://test.com" # Include full HTTP/HTTPS URL for your SharePoint site
$SourceLibraryName = "Documents1"
$TargetFolderRelativeURL = "/Documents1/Redundant"
$FileListPath = "F:\users\filelist.txt"

# Load SharePoint PowerShell Snapin (if not already loaded)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Get the SharePoint Web object
$Web = Get-SPWeb $WebURL -ErrorAction Stop

try {
    # Retrieve the source document library and its root folder
    $SourceLibrary = $Web.Lists[$SourceLibraryName]
    $SourceRootFolder = $SourceLibrary.RootFolder

    # Validate the target Redundant folder exists
    $TargetFolder = $Web.GetFolder($TargetFolderRelativeURL)
    if (-not $TargetFolder.Exists) {
        Write-Error "Target folder '$TargetFolderRelativeURL' does not exist! Exiting script."
        exit 1
    }

    # Read the list of filenames to migrate
    $filenames = Get-Content $FileListPath

    foreach ($fileName in $filenames) {
        # Skip empty lines in the text file
        if ([string]::IsNullOrWhiteSpace($fileName)) {
            continue
        }

        # Find all matching files in the source library (recursive across all subfolders)
        $filesToMove = $SourceLibrary.Items | Where-Object {
            $_.Name -eq $fileName -and $_.FileSystemObjectType -eq "File"
        }

        if ($filesToMove.Count -eq 0) {
            Write-Warning "File '$fileName' not found in the Documents1 library. Skipping."
            continue
        }

        # Move each matching file to the target folder
        foreach ($fileItem in $filesToMove) {
            try {
                $file = $fileItem.File
                # Use MoveTo() with $true to overwrite existing files (set to $false if you want to avoid overwrites)
                $file.MoveTo("$($TargetFolder.Url)/$($file.Name)", $true)
                Write-Output "Successfully moved: $($file.Name)"
            }
            catch {
                Write-Error "Failed to move '$fileName': $_"
            }
        }
    }
}
finally {
    # Critical: Dispose the SPWeb object to prevent memory leaks
    $Web.Dispose()
}

Key Improvements & Notes

  • SharePoint Object Model: Uses native SharePoint cmdlets and methods (Get-SPWeb, SPFile.MoveTo()) instead of local file system commands, which is the only way to reliably interact with SharePoint on-premises.
  • Error Handling: Includes checks for missing target folders, missing files, and catches move failures to avoid script crashes.
  • Memory Management: The try/finally block ensures the SPWeb object is properly disposed, which prevents memory leaks in long-running scripts.
  • Overwrite Control: The $true parameter in MoveTo() lets you overwrite existing files in the Redundant folder—change this to $false if you want to skip files that already exist.
  • Permissions: Make sure the account running the script has Edit permissions on both the source document library and the Redundant folder.

Important Caveat

This script works for SharePoint On-Premises. If you're using SharePoint Online, you'll need to switch to PnP PowerShell instead of the Server Object Model.

内容的提问来源于stack exchange,提问作者ish_dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:38:12