SharePoint Online: ย้ายไฟล์ทั้งหมดจากโฟลเดอร์หนึ่งไปยังอีกโฟลเดอร์หนึ่งโดยใช้ PowerShell
ความต้องการ: ย้ายไฟล์ทั้งหมดระหว่างโฟลเดอร์ใน SharePoint Online โดยใช้ PowerShell
จะย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online ได้อย่างไร
ใน SharePoint Online โฟลเดอร์มีบทบาทสำคัญในการจัดระเบียบและจัดหมวดหมู่เนื้อหา ทำให้ค้นหาสิ่งที่คุณต้องการได้ง่ายขึ้น อย่างไรก็ตาม เมื่อองค์กรของคุณเติบโตและพัฒนา ความจำเป็นในการย้ายไฟล์ระหว่างโฟลเดอร์ก็อาจเกิดขึ้นได้ ซึ่งสามารถทำได้ด้วยตนเองโดยการเลือกไฟล์และเลือก "ย้ายไปที่" ผ่านทางอินเทอร์เฟซผู้ใช้ SharePoint อย่างไรก็ตาม หากคุณมีไฟล์หลายไฟล์ที่ต้องย้าย หรือย้ายไฟล์ซ้ำๆ กระบวนการนี้อาจใช้เวลานาน โชคดีที่มีวิธีทำให้งานการย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online เป็นแบบอัตโนมัติโดยใช้ PowerShell ในคู่มือนี้ เราจะสำรวจทั้งสองวิธีและแสดงวิธีย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online
เมื่อต้องการย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online ด้วย SharePoint UI คุณสามารถทำตามขั้นตอนต่อไปนี้:
- นำทางไปยังโฟลเดอร์ต้นฉบับใน SharePoint Online แล้วเลือกไฟล์ที่คุณต้องการย้าย
เลือกไฟล์ที่จะย้ายและคลิกที่ปุ่ม "ย้ายไปที่" จากแถบคำสั่ง
- เลือกโฟลเดอร์ปลายทางที่คุณต้องการย้ายไฟล์ไป
คลิกที่ปุ่ม "ย้ายมาที่นี่"
นี่เป็นวิธีที่ง่ายและตรงไปตรงมาที่สุดในการย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online
PowerShell เพื่อย้ายไฟล์ทั้งหมดจากโฟลเดอร์หนึ่งไปยังอีกโฟลเดอร์หนึ่งใน SharePoint Online:
ถ้าคุณใช้ SharePoint Online มาเป็นระยะเวลาหนึ่ง คุณอาจคุ้นเคยกับกระบวนการอัปโหลดและดาวน์โหลดไฟล์พื้นฐาน อย่างไรก็ตาม อาจมีบางครั้งที่คุณต้องย้ายไฟล์ระหว่างโฟลเดอร์ในไซต์ของคุณ เมื่อคุณย้ายไฟล์และโฟลเดอร์ไปมาใน SharePoint Online บ่อยครั้ง การเปิดไซต์ SharePoint Online อาจยุ่งยากเล็กน้อย นำทางไปยังโฟลเดอร์ที่คุณต้องการย้ายไฟล์หรือโฟลเดอร์ แล้วย้ายเข้าที่
จะดีกว่าไหมถ้ามีวิธีง่ายๆ ในการทำเช่นนี้จาก PowerShell บทความนี้จะแสดงวิธีใช้ PowerShell เพื่อย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online อย่างรวดเร็วและง่ายดาย มาเริ่มกันเลย!
จะย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online ได้อย่างไร นี่คือ PowerShell ที่จะย้ายไฟล์ทั้งหมดจากโฟลเดอร์หนึ่งไปยังอีกโฟลเดอร์หนึ่ง:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Relative URLs of Source and Destination Folders
$SourceFolderURL = "/sites/marketing/Shared Documents"
$TargetFolderURL = "/sites/marketing/Shared Documents/Archive"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get all items from source folder
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$Query.FolderServerRelativeUrl = $SourceFolderURL
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Move All Files from Source to Destination
ForEach ($Item in $ListItems)
{
#Filter Files
If($Item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType]::File)
{
$TargetFileUrl = $Item["FileRef"].ToString().Replace($SourceFolderURL,$TargetFolderURL)
$Item.File.MoveTo($TargetFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
$Ctx.ExecuteQuery()
Write-host -f Green "File Moved to:"$TargetFileUrl
}
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
สคริปต์นี้จะย้ายไฟล์ทั้งหมดจากโฟลเดอร์ต้นทางไปยังโฟลเดอร์เป้าหมาย จะเกิดอะไรขึ้นถ้าโฟลเดอร์ต้นทางมีโฟลเดอร์ย่อย?
SharePoint Online: ย้ายไฟล์และโฟลเดอร์โดยใช้ PowerShell
สคริปต์ PowerShell นี้จะย้ายไฟล์และโฟลเดอร์ย่อยทั้งหมดระหว่างโฟลเดอร์ที่ระบุในไลบรารีเอกสาร SharePoint Online นอกจากนี้เรายังสามารถย้ายโฟลเดอร์ระหว่างไลบรารีเอกสารหรือไซต์ด้วย PnP PowerShell ได้ เช่นเดียวกับใน วิธีย้ายโฟลเดอร์ใน SharePoint Online
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Move-SPOFilesBetweenFolders
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
#Write-host "Copying Files from '$($SourceFolder.ServerRelativeUrl)' to '$($TargetFolder.ServerRelativeUrl)'"
#Get all Files from the source folder
$SourceFilesColl = $SourceFolder.Files
$Ctx.Load($SourceFilesColl)
$Ctx.ExecuteQuery()
#Iterate through each file and move
Foreach($SourceFile in $SourceFilesColl)
{
#Get all files from source Folder
$SourceFile =$Ctx.Web.GetFileByServerRelativeUrl($SourceFile.ServerRelativeUrl)
$Ctx.Load($SourceFile)
$Ctx.ExecuteQuery()
#Move File to destination
$TargetFileUrl = $SourceFile.ServerRelativeUrl -Replace $SourceFolderURL,$TargetFolderURL
$SourceFile.MoveTo($TargetFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
$Ctx.ExecuteQuery()
Write-host -f Green "File Moved to: "$TargetFileURL
}
#Process Sub Folders
$SubFolders = $SourceFolder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
If($SubFolder.Name -ne "Forms")
{
#Prepare Target Folder
$EnsureFolderURL = $SubFolder.ServerRelativeUrl -Replace $SourceFolderUrl, $TargetFolderUrl
Try {
$Folder=$Ctx.web.GetFolderByServerRelativeUrl($EnsureFolderURL)
$Ctx.load($Folder)
$Ctx.ExecuteQuery()
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$Folder=$Ctx.Web.Folders.Add($EnsureFolderURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
Write-host "New Folder Created:"$SubFolder.Name -f Yellow
}
}
#Call the function recursively to move all files from source folder to target
Move-SPOFilesBetweenFolders -SiteURL $SiteURL -SourceFolder $SubFolder -TargetFolder $Folder
#Remove the Source Folder
$SubFolder.Recycle() | Out-Null
$Ctx.ExecuteQuery()
}
}
}
Catch {
write-host -f Red "Error Moving File:" $_.Exception.Message
}
}
#Set Parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SourceFolderURL ="/sites/marketing/shared Documents/2018"
$TargetFolderURL ="/sites/marketing/Shared Documents/2019"
#Setup Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the source and Target Folders
$SourceFolder=$Ctx.Web.GetFolderByServerRelativeUrl($SourceFolderURL)
$Ctx.Load($SourceFolder)
$TargetFolder=$Ctx.Web.GetFolderByServerRelativeUrl($TargetFolderURL)
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
#Call the function
Move-SPOFilesBetweenFolders -SiteURL $SiteURL -SourceFolder $SourceFolder -TargetFolder $TargetFolder
ถ้าคุณต้องการย้ายไฟล์เดียว ให้ใช้: SharePoint Online: ย้ายไฟล์ระหว่างไลบรารีเอกสารโดยใช้ PowerShell
PnP PowerShell เพื่อย้ายไฟล์และโฟลเดอร์ทั้งหมดระหว่างไลบรารีเอกสาร
PnP PowerShell ทำให้ง่ายขึ้นมาก! คุณสามารถย้ายเนื้อหาไลบรารีเอกสารไปยังไซต์ SharePoint Online อื่นด้วยสคริปต์ PowerShell นี้:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Purchase"
$SourceLibraryURL = "Migration" #Site Relative URL from the current site
$TargetLibraryURL = "/sites/Marketing/Migration" #Server Relative URL of the Target Folder
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get all Items from the Document Library
$Items = Get-PnPFolderItem -FolderSiteRelativeUrl $SourceLibraryURL | Where {$_.Name -ne "Forms"}
#Move All Files and Folders Between Document Libraries
Foreach($Item in $Items)
{
Move-PnPFile -SourceUrl $Item.ServerRelativeUrl -TargetUrl $TargetLibraryURL -AllowSchemaMismatch -Force -AllowSmallerVersionLimitOnDestination
Write-host "Moved Item:"$Item.ServerRelativeUrl
}
ตรวจสอบให้แน่ใจว่าคุณได้สร้างไลบรารีเป้าหมายก่อนที่จะรันสคริปต์นี้!
บทสรุป:
โดยสรุป SharePoint มีความสามารถในการจัดระเบียบเนื้อหาลงในโฟลเดอร์ ทำให้ง่ายต่อการค้นหาสิ่งที่คุณต้องการ เมื่อจำเป็นต้องย้ายไฟล์ระหว่างโฟลเดอร์ ก็สามารถทำได้ด้วยการคลิกเพียงไม่กี่ครั้ง เนื่องจากการย้ายไฟล์ระหว่างโฟลเดอร์ใน SharePoint Online ทำได้ง่ายขึ้นมากในตอนนี้! นอกจากนี้ยังเป็นไปได้ที่จะทำให้งานนี้เป็นแบบอัตโนมัติโดยใช้ PowerShell เนื่องจากมีประสิทธิภาพมากกว่า ด้วยโค้ดเพียงไม่กี่บรรทัด คุณสามารถทำให้กระบวนการจัดระเบียบเนื้อหาของคุณเป็นแบบอัตโนมัติได้


