Copy-TeamsUserTeamsAndChannels.psm1
function Copy-TeamsUserTeamsAndChannels { <# .SYNOPSIS Copies Microsoft Teams and channels from one user to another user, with optional settings. .DESCRIPTION This function allows you to copy Microsoft Teams memberships and channels from a source user to a new user. You can choose to set the new user as an owner or member of the teams, and optionally copy the channels as well. .PARAMETER SourceUserEmail The email address of the source user whose Teams and channels will be copied. .PARAMETER NewUserEmail The email address of the new user who will be added to the Teams and channels. .PARAMETER Owner Switch parameter. If provided, the new user will be added as an owner of the Teams (default role is member). .PARAMETER Channel Switch parameter. If provided, the function will prompt the user to choose Channels for each Team to add. .NOTES Author: Eric Meinders Version: 1.0 #> [alias("Copy-TeamsAndChannels","Copy-TAC")] [cmdletbinding(SupportsShouldProcess, ConfirmImpact = "High")] param ( [parameter(Mandatory, Position = 1)][string]$SourceUserEmail, [parameter(Mandatory, Position = 2)][string]$NewUserEmail, [switch]$Owner, #switch for setting the Teams role to owner, default is member [switch]$Channel #switch for prompting the user to choose Channels for each Team to add ) BEGIN #verify MicrosoftTeams module is installed and connected { $Role = "member" try #checks if MicrosoftTeams module installed { Get-InstalledModule -Name MicrosoftTeams -ErrorAction Stop > $null } catch #if MicrosoftTeams is not found, prompts user to install { Write-Warning "MicrosoftTeams module not found." if ($PScmdlet.ShouldProcess("MicrosoftTeams","Install-Module")) { Install-Module -Name MicrosoftTeams -Force } else { throw "MicrosoftTeams module required for function. Exiting." } } try #checks if connection exists to MicrosoftTeams { Get-CsTenant > $null } catch #prompts for connection if not connected { Write-Verbose "Connecting to Microsoft 365" Connect-MicrosoftTeams } if ($Owner) #validates if -Owner switch discovered, sets role to owner { $Role = "owner" } if ($Owner -and $Channels) #throws if both -Owner and -Channels switches provided, cannot set channels to owner { throw "Cannot add Owner role with Channels argument." } } PROCESS #process the teams and channels query/addition { $userTeams = Get-Team -User $SourceUserEmail #get all teams the source user belongs to foreach ($team in $userTeams) { try { Write-Verbose "Current working Team: $($team.DisplayName)" if ($PScmdlet.ShouldProcess($NewUserEmail,"Adding to $($team.DisplayName)")) { Add-TeamUser -GroupId $team.GroupId -User $NewUserEmail -Role $Role -Verbose if ($Channels) { #get teams groupid, iterates through and adds new user Get-TeamChannel -GroupId $team.GroupId | ForEach-Object { Write-Verbose "Current working Channel: $($_.DisplayName)" if ($PScmdlet.ShouldProcess($NewUserEmail,"Adding to channel: $($_.DisplayName)")) { try { $parameters = @{ GroupId = $team.GroupId DisplayName = $_.DisplayName User = $NewUserEmail Verbose = $true } Add-TeamChannelUser @parameters } catch { Write-Warning $_.exception.message } } else { Write-Output "Skipping $($_.DisplayName) for user ${NewUserEmail}" } } } } else { Write-Output "Skipping $($team.DisplayName) for user ${NewUserEmail}" } } catch { Write-Warning $_.exception.message } } } } |