编写PowerShell脚本迁移SharePoint文档库指定文件至冗余文件夹
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/finallyblock ensures theSPWebobject is properly disposed, which prevents memory leaks in long-running scripts. - Overwrite Control: The
$trueparameter inMoveTo()lets you overwrite existing files in the Redundant folder—change this to$falseif 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
相关产品推荐
相关产品推荐

