Managing Microsoft Teams Using the Microsoft Graph PowerShell

October 29, 2021
4 min read
Syndicated

With Microsoft Teams being as popular as it is now, it is even more important for administrators to manage it effectively. Microsoft Teams provides a PowerShell module available for install using the following command:

Install-Module MicrosoftTeams
Import-Module MicrosoftTeams

You can, however, use the Microsoft Graph PowerShell instead to perform many of the same administration tasks. You can find the documentation here:

https://docs.microsoft.com/en-us/powershell/module/microsoft.graph.teams/?view=graph-powershell-beta

As always, to install the Microsoft Graph PowerShell modules, you can use these commands:

Install-Module Microsoft.Graph
Select-MgProfile -Name "beta"
Import-Module Microsoft.Graph

Next, you need to connect to the Microsoft Graph with the specific scopes or permissions for managing Microsoft Teams.

$scopes = @(
	"TeamsApp.ReadWrite.All",
	"TeamsAppInstallation.ReadWriteForTeam",
	"TeamsAppInstallation.ReadWriteSelfForTeam",
	"TeamSettings.ReadWrite.All",
	"TeamsTab.ReadWrite.All",
	"TeamMember.ReadWrite.All",
	"Group.ReadWrite.All",
	"GroupMember.ReadWrite.All"
)

Connect-MgGraph -Scopes $scopes

Once connected, you can start to execute commands. For these examples to work, I have already retrieved the Team ID using other PowerShell.

$object = Get-Team -DisplayName "SOC Team"

Some examples are below:

NOTE: No comments on the use or lack of use of backticks, splatting, or class instantiation either; it is just for layout on the post 🙂

View Microsoft Team Details

$team = Get-MgTeam -TeamID $object.GroupId

$team | Select-Object *

View Microsoft Team Channels

$channels = Get-MgTeamChannel -TeamId $object.GroupId

$channels | Select-Object *

View Microsoft Team Members

$members = Get-MgTeamMember -TeamId $object.GroupId

$members | Select-Object *

Create a New Microsoft Team

$teamname = "PowerShell Team"
$teamdescription = "PowerShell Team Description"
$teamvisibility = "public"

$funSettings = @{ 
		"allowGiphy" = "false"; 
		"giphyContentRating" = "strict"; 
		"allowStickersAndMemes" = "false"; 
		"allowCustomMemes" = "false"; 
}

$memberSettings =  @{ 
	"allowCreateUpdateChannels" = "true"; 
	"allowCreatePrivateChannels" = "false"; 
	"allowDeleteChannels" = "true"; 
	"allowAddRemoveApps" = "false"; 
	"allowCreateUpdateRemoveTabs" = "false"; 
	"allowCreateUpdateRemoveConnectors" = "false"; 
}

$guestSettings = @{ 
        "allowCreateUpdateChannels" = "false"; 
        "allowDeleteChannels" = "false"; 
}

$messagingSettings = @{ 
	"allowUserEditMessages" = "true"; 
        "allowUserDeleteMessages" = "true" ;
        "allowOwnerDeleteMessages" = "false"; 
        "allowTeamMentions" = "false"; 
        "allowChannelMentions" = "false"; 
}

Using Namespace Microsoft.Graph.PowerShell.Models
[MicrosoftGraphTeam1]@{
	Template = [MicrosoftGraphTeamsTemplate]@{
        	Id = 'com.microsoft.teams.template.OrganizeHelpDesk'
    	}
	DisplayName  = $teamname
	Description = $teamdescription
	FunSettings  = $funSettings
	GuestSettings  = $guestSettings
	MessagingSettings = $messagingSettings
	MemberSettings = $memberSettings
	Visibility = $teamvisibility
} | New-MgTeam

Update a Microsoft Team

$funSettings = @{ `
		"allowGiphy" = "true"; `
		"giphyContentRating" = "moderate"; `
		"allowStickersAndMemes" = "true"; `
		"allowCustomMemes" = "true"; `
}
Update-MgTeam `
	-TeamId $object.GroupId `
	-FunSettings $funSettings

Add a Teams Channel

$channelname = "Commands"
$channeldescription = "Channel for Suggested Commands"

$channel = New-MgTeamChannel `
		-TeamId $object.GroupId `
		-DisplayName $channelname `
		-Description $channeldescription

Update Teams Channel

$channelname = "Cmdlets"
$channeldescription = "Channel for Suggested Cmdlets"

Update-MgTeamChannel `
	-ChannelId $channel.Id `
	-TeamId $object.GroupId `
	-DisplayName $channelname `
	-Description $channeldescription

Add a Teams Owner and Member

$owneruser = "adelev@M365x.OnMicrosoft.com"

$ownerteamuser = Get-MgUser `
	-UserId $user

$ownerproperties = @{
	"@odata.type" = "#microsoft.graph.aadUserConversationMember";
	"user@odata.bind" = "https://graph.microsoft.com/beta/users/" + $ownerteamuser.Id
}

$role = "owner"

New-MgTeamMember `
	-TeamId $object.GroupId `
	-Roles $role `
	-AdditionalProperties $ownerproperties
$memberuser = "adelev@M365x.OnMicrosoft.com"

$memberteamuser = Get-MgUser `
	-UserId $user

$memberproperties = @{
	"@odata.type" = "#microsoft.graph.aadUserConversationMember";
	"user@odata.bind" = "https://graph.microsoft.com/beta/users/" + $memberteamuser.Id
}

$role = "member"

New-MgTeamMember `
	-TeamId $object.GroupId `
	-Roles $role `
	-AdditionalProperties $memberproperties

Archive and Unarchive a Team

Invoke-MgArchiveTeam `
		-TeamId $object.GroupId

Invoke-MgUnarchiveTeam `
		-TeamId $object.GroupId

There are many more commands available; however, you will need to spend time within the Graph Explorer checking each command to ensure what format is required and if it will function as expected.

As the documentation updates, examples will be available to help in executing all of the commands.

Liam Cleary

Liam Cleary

Liam began his career as a computer trainer. He quickly realized that programming, breaking and hacking were much more fun. Liam spent the next few years working within core infrastructure and security services. He is now the founder and owner of SharePlicity, a consulting company focusing on Microsoft 365 and Azure technology. His role within SharePlicity is to help organizations implement Microsoft 365 and Azure technology to enhance internal and external collaboration, document, and records management, automate business processes, and implement security controls and protection. He is a long-time Microsoft MVP and Microsoft Certified Trainer, focusing on architecture, security and crossing the boundary into software development. Over the past few years, his specialty has been security in Microsoft 365, Azure and its surrounding platforms. Liam also creates online training courses for Pluralsight, LinkedIn Learning and Cloud Academy, and he teaches multiple Microsoft certification courses for Opsgility and Microsoft. You can find him at user groups and conferences, teaching classes, offering advice, spending time in the community, teaching his kids how to code, raspberry PI programming, hacking the planet, building Lego robots, or coaching soccer. You may also find him running races in the dark, hiking, or mountain biking at breakneck speeds.