当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 在文档库之间移动文件

作者:精品下载站 日期:2024-12-14 14:56:49 浏览:17 分类:玩电脑

SharePoint Online:使用 PowerShell 在文档库之间移动文件


要求: SharePoint Online PowerShell 将文件从一个库移动到另一个库。

如何在 SharePoint Online 中移动文件?

您是否正在寻找在 SharePoint Online 站点之间移动文件的方法?也许您需要将文件从文档库移至文件区域。或者您可能需要一次移动大量文件?

无论如何,以下是最终用户如何在 SharePoint Online 中的文档库之间移动文件:

  1. 导航到您的 SharePoint Online 文档库。
  2. 选择要移动的文件旁边的复选框 >> 单击命令栏中的“移动到”。 (右键单击文档,或单击省略号图标并选择“移至”作为替代方法)。

    [玩转系统] SharePoint Online:使用 PowerShell 在文档库之间移动文件

  3. 这将打开右侧的信息面板。选择您的文件需要移动到的目标库。您可以选择当前库中的任何文件夹、当前网站上的任何其他库,甚至不同网站集中的库。

    [玩转系统] SharePoint Online:使用 PowerShell 在文档库之间移动文件

  4. 选择要移动文档的目标位置,然后单击“移动到此处”按钮开始移动文件。您还可以通过单击右上角的文件夹图标在目标中创建新文件夹。

    [玩转系统] SharePoint Online:使用 PowerShell 在文档库之间移动文件

您将在工具栏中看到“正在移动”消息,并且您的文件将立即从源站点移动到选定的目标站点。此方法可以在 SharePoint 文档库之间传输文件或在网站集(包括自己的 OneDrive)之间移动文件。如果您没有看到目标下列出的站点,则自定义脚本可能未在目标站点上启用。这是跨站复制的前提!因此,请从 SharePoint 管理员凭据启用它。

请注意移动功能的限制:总文件大小不超过 100 GB。单次操作不超过30,000个文件。每个文件必须小于 15 GB!

值得一提的是,这个功能在SharePoint的经典体验中是不具备的。虽然您可以使用 Windows 资源管理器中的“资源管理器视图/文件资源管理器”移动文件,但您将丢失版本历史记录(仅复制最新版本)、自定义元数据等。

如何在 SharePoint 中拖动和移动文件?

您知道只需通过拖放即可移动文件和整个文件夹吗?您可以通过选择、拖动它们并将它们放到面包屑中同一级别或不同级别的任何文件夹来移动多个文件!

[玩转系统] SharePoint Online:使用 PowerShell 在文档库之间移动文件

让我们看看如何使用PowerShell快速移动文件!

使用 PowerShell 移动 SharePoint Online 文件

以下是用于在 SharePoint 站点之间移动文档的 SharePoint Online PowerShell:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#powershell script to move files in sharepoint online - Function
Function Move-SPOFile([String]$SiteURL, [String]$SourceFileURL, [String]$TargetFileURL)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #sharepoint online powershell to move files
        $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
        $Overwrite = $True
        [Microsoft.SharePoint.Client.MoveCopyUtil]::MoveFile($Ctx, $SourceFileURL, $TargetFileURL, $Overwrite, $MoveCopyOpt)
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "File Moved Successfully!"
    }
    Catch {
    write-host -f Red "Error Moving the File!" $_.Exception.Message
    }
}
 
#Set Config Parameters
$SiteURL="https://Crescent.sharepoint.com/sites/Marketing"
$SourceFileURL="https://Crescent.sharepoint.com/sites/Marketing/Shared Documents/Discloser Asia.doc"
$TargetFileURL="https://Crescent.sharepoint.com/Shared Documents/Discloser Asia.doc"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Call the function to Move the File
Move-SPOFile $SiteURL $SourceFileURL $TargetFileURL

此 PowerShell 可以将文件传输到另一个库或网站集之间。

PowerShell 在同一站点的文档库之间移动文件:

您可以在 SharePoint Online 中使用此 PowerShell 将文件移动到网站上的另一个库。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL="https://crescent.sharepoint.com"
$FileURL="/Project Documents/Active Users.xlsx" #Server Relative Path to the source File
$DestFileURL="https://crescent.sharepoint.com/Project Docs/Active.xlsx"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)

#sharepoint online powershell move documents
$File.MoveTo($DestFileURL, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite) 
$Ctx.ExecuteQuery()

用于在 SharePoint Online 中移动文件的 PnP PowerShell

以下是用于在 SharePoint Online 中的文件夹之间移动文件的 PnP PowerShell cmdlet Move-PnPFile: 此 PowerShell 将文件“Recipient KSA.pdf”从“共享文档”移动到同一库的子文件夹“Active”。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$SourceURL= "Shared Documents/Recipient KSA.pdf"
$TargetURL = "Shared Documents/Active/Recipient KSA.pdf"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#move files in sharepoint online using powershell
Move-PnPFile -SiteRelativeUrl $SourceURL -TargetUrl $TargetURL -Force

同样,您也可以将文件移动到另一个库或在网站集之间移动文件。请注意,如果目标 URL 中指定的文件名已存在,则不会执行移动。使用 -OverwriteIfAlreadyExists 覆盖! “AllowSchemaMismatch”开关确保目标库可以拥有一组不同的元数据列。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$SourceFileURL= "Branding/Technical Design.docx" #Site Relative URL from the current site
$TargetFileURL = "/sites/Purchase/Shared Documents" #Server Relative URL of the Target Folder

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#move file between sites in sharepoint online using powershell
Move-PnPFile -SiteRelativeUrl $SourceFileURL -TargetUrl $TargetFileURL -Force -AllowSchemaMismatch

附带说明,您需要足够的权限才能在 SharePoint Online 中移动文件。此外,如果以前在其他位置共享或链接过文件,则移动文件可能会破坏该文件的链接。如果要在文件夹之间移动所有文件,请使用:SharePoint Online:使用 PowerShell 将所有文件从一个文件夹移动到另一个文件夹

  • SharePoint Online:如何使用 PowerShell 移动文件夹?
  • SharePoint Online:使用 PowerShell 将子网站移动到另一个网站
  • SharePoint Online:将用户从一个组复制/移动到另一个组
  • 使用元数据和版本历史记录在文档库之间移动文件

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯