使用 PowerShell 在 SharePoint 中的文档库之间复制文件
要求:使用 PowerShell 在 SharePoint 文档库之间复制文件
将文件从一个库复制到另一个库的 PowerShell 脚本
在这篇博文中,我们将向您展示如何使用 PowerShell 在 SharePoint 文档库之间复制文件。如果您想要在两个或多个文档库之间快速复制或移动文件以进行备份或存档,这会很有用。
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables for Processing
$WebURL="https://portal.crescent.com/sites/Sales/"
$SourceFile="https://portal.crescent.com/sites/Sales/TeamDocs/InvoiceTemp.xlsx"
$TargetLibrary="Invoice Documents"
#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFile = $Web.GetFile($SourceFile)
$TargetLibrary = $Web.GetFolder($TargetLibrary)
#Copy the file into the Target library
$File = $TargetLibrary.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(), $true)
#Copy Meta-Data
$Item = $File.Item
$item["Created"] = $SourceFile.TimeCreated.ToLocalTime()
$item["Modified"] = $SourceFile.TimeLastModified.ToLocalTime()
$item["Author"] = $SourceFile.Author
$item["Editor"] = $SourceFile.ModifiedBy
#Update
$Item.UpdateOverwriteVersion()
用于在文档库之间复制所有文件的 PowerShell 脚本:
让我们将所有文件和文件夹及其元数据从一个库复制到另一个库(不包括:上面的!)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom Function to Copy Files from Source Folder to Target
Function Copy-Files($SourceFolder, $TargetFolder)
{
write-host "Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)"
#Get Each File from the Source
$SourceFilesColl = $SourceFolder.Files
#Iterate through each item from the source
Foreach($SourceFile in $SourceFilesColl)
{
#Copy File from the Source
$NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)
#Copy Meta-Data from Source
Foreach($Field in $SourceFile.Item.Fields)
{
If(!$Field.ReadOnlyField)
{
if($NewFile.Item.Fields.ContainsField($Field.InternalName))
{
$NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
}
}
}
#Update
$NewFile.Item.UpdateOverwriteVersion()
Write-host "Copied File:"$SourceFile.Name
}
#Process SubFolders
Foreach($SubFolder in $SourceFolder.SubFolders)
{
if($SubFolder.Name -ne "Forms")
{
#Check if Sub-Folder exists in the Target Library!
$NewTargetFolder = $TargetFolder.ParentWeb.GetFolder($SubFolder.Name)
if ($NewTargetFolder.Exists -eq $false)
{
#Create a Folder
$NewTargetFolder = $TargetFolder.SubFolders.Add($SubFolder.Name)
}
#Call the function recursively
Copy-Files $SubFolder $NewTargetFolder
}
}
}
#Variables for Processing
$WebURL="https://portal.crescent.com/sites/sales/"
$SourceLibrary ="Team Docs"
$TargetLibrary = "Sales Documents"
#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFolder = $Web.GetFolder($SourceLibrary)
$TargetFolder = $Web.GetFolder($TargetLibrary)
#Call the Function to Copy All Files
Copy-Files $SourceFolder $TargetFolder
使用 PowerShell 在文件夹之间复制文件
想要在文档库中的文件夹之间复制文件吗?只需将参数值 $SoureLibrary 和 $TargetLibrary 更改为文件夹 URL。例如。
#Relative Path of the Folder
$SourceLibrary ="/Documents/2017"
$TargetLibrary = "/Team Documents/Active/2018"
在网站、网站集、Web 应用程序之间复制文档:
在站点之间复制怎么样?好吧,只需更改脚本中的以下几行即可。
#Variables for Processing
$SourceWebURL = "https://Your-Source-Web-URL"
$TargetWebURL = "https://Your-Target-Web-URL"
$SourceWeb = Get-SPWeb $SourceWebURL
$TargetWeb = Get-SPWeb $TargetWebURL
$SourceLibrary ="Team Docs"
$TargetLibrary = "Shared Documents"
$SourceFolder = $SourceWeb.GetFolder($SourceLibrary)
$TargetFolder = $TargetWeb.GetFolder($TargetLibrary)
#Call the Function to Copy All Files
Copy-Files $SourceFolder $TargetFolder
要在 SharePoint 文档库之间移动文件,请使用:在包含元数据和版本历史记录的文档库之间移动文件
