Last active
June 29, 2023 19:05
-
-
Save dpo007/9d2813cd3303ae1908657e644950758b to your computer and use it in GitHub Desktop.
PowerShell :: Move all folders between Teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 5.0 | |
#Requires -Modules SharePointPnPPowerShellOnline | |
<# | |
.SYNOPSIS | |
Script to move folders and files from a source team's site to a destination team's site in SharePoint Online. | |
.DESCRIPTION | |
This PowerShell script requires PowerShell version 5.0 and the SharePointPnPPowerShellOnline module. It connects to SharePoint Online and retrieves the source team's site and the destination team's site based on the provided team names. It then retrieves all folders and files in the source team's site and moves each item to the destination team's site. | |
.PARAMETER SrcTeam | |
The name of the source team. Default: "Source Team Name" | |
.PARAMETER DstTeam | |
The name of the destination team. Default: "Destination Team Name" | |
#Requires -Version 5.0 | |
#Requires -Modules SharePointPnPPowerShellOnline | |
.PARAMETER SrcTeam | |
The name of the source team. Default: "Source Team Name" | |
.PARAMETER DstTeam | |
The name of the destination team. Default: "Destination Team Name" | |
.EXAMPLE | |
.\Move-FilesToDestination.ps1 -SrcTeam "Marketing" -DstTeam "Sales" | |
Connects to SharePoint Online and moves all folders and files from the "Marketing" team's site to the "Sales" team's site. | |
.EXAMPLE | |
.\Move-FilesToDestination.ps1 | |
Connects to SharePoint Online and moves all folders and files from the default source team's site to the default destination team's site. | |
#> | |
param( | |
[string]$SrcTeam = "Source Team Name", | |
[string]$DstTeam = "Destination Team Name" | |
) | |
# Connect to SharePoint Online | |
try { | |
Connect-PnPOnline -Url https://tenant.sharepoint.com | |
} catch { | |
Write-Error "Error connecting to SharePoint Online: $_" | |
exit | |
} | |
# Get the source team's site | |
try { | |
$srcSite = Get-PnPTenantSite -Filter "GroupId eq '$SrcTeam'" | |
} catch { | |
Write-Error "Error getting the source team's site: $_" | |
exit | |
} | |
# Get the destination team's site | |
try { | |
$dstSite = Get-PnPTenantSite -Filter "GroupId eq '$DstTeam'" | |
} catch { | |
Write-Error "Error getting the destination team's site: $_" | |
exit | |
} | |
# Get all folders and files in the source team's site | |
try { | |
$items = Get-PnPListItem -List "Shared Documents" -Folder | |
} catch { | |
Write-Error "Error getting items from the source team's site: $_" | |
exit | |
} | |
# Move each item to the destination team's site | |
foreach ($item in $items) { | |
try { | |
Move-PnPListItem -SourceUrl $item.Url -TargetUrl $dstSite.Url | |
} catch { | |
Write-Error "Error moving item $($item.Name) to the destination team's site: $_" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment