New-MgTeam: How to Create Microsoft Teams with Graph PowerShell

Learn how to use the New-MgTeam cmdlet in Microsoft Graph PowerShell to create Microsoft Teams with properties like display name, description, and visibility. Includes examples for single and bulk creation.

The New-MgTeam cmdlet is a powerful tool for creating and managing Microsoft Teams through PowerShell. This article provides a detailed guide on using this cmdlet, including syntax, usage examples, tips, and solutions to common errors.


Prerequisites

  • You need a: Teams Administrator..
  • Install the Graph PowerShell module by running the command: Install-Module Microsoft.Graph -Scope CurrentUser.
  • Connect to Graph PowerShell with the scope 'Group.ReadWrite.All' by running the command: Connect-MgGraph -Scopes "Group.ReadWrite.All".

Cmdlet Syntax

New-MgTeam -BodyParameter <hashtable>
  • New-MgTeam: The cmdlet to create a new Microsoft Team.
  • -BodyParameter: This parameter takes a hashtable that defines the properties of the team.
  • <hashtable>: A hashtable is a collection of key-value pairs. In this context, it specifies the properties of the team, such as the team's name, description, visibility, and other settings.

Usage Examples

Basic Team Creation

The command for creating a team with only basic details like DisplayName, Description, Visibility and the team template.

$team = @{
    DisplayName = "Team Name"
    Description = "This is a sample team"
    Visibility = "Public"
    "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
}

New-MgTeam -BodyParameter $team

Creating a Team with Specific Settings

The command for creating a team with specific member settings, guest settings, messaging settings and fun settings.

$team = @{
    DisplayName = "Team Name"
    Description = "This is a sample team with specific settings"
    Visibility = "Private"
    "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
    MemberSettings = @{
        AllowCreateUpdateChannels = $true
        AllowDeleteChannels = $false
    }
    GuestSettings = @{
        AllowCreateUpdateChannels = $false
        AllowDeleteChannels = $false
    }
    MessagingSettings = @{
        AllowUserEditMessages = $true
        AllowUserDeleteMessages = $false
    }
    FunSettings = @{
        AllowGiphy = $true
        GiphyContentRating = "Moderate"
    }
}

New-MgTeam -BodyParameter $team

Bulk Creating Teams from a CSV File

The command for creating multiple teams by reading data from CSV files. Ensure your CSV files contain the following headers.

Example CSV File:

DisplayName,Description,Visibility
Team1,Description for Team1,Private
Team2,Description for Team2,Public

PowerShell script to create teams from the CSV file:

$teams = Import-Csv -Path "C:\path\to\teams.csv"

foreach ($team in $teams) {
    $teamParams = @{
        DisplayName = $team.DisplayName
        Description = $team.Description
        Visibility = $team.Visibility
        "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
    }
    New-MgTeam -BodyParameter $teamParams
}

Creating a Team from an Existing Microsoft 365 Group

You can also create a team from an existing Microsoft 365 group by passing the Group Id to group@odata.bind parameter.

$params = @{
    "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
    "group@odata.bind" = "https://graph.microsoft.com/v1.0/groups('your-group-id')"
}

New-MgTeam -BodyParameter $params

Replace 'your-group-id' with the actual ID of the Microsoft 365 Group you want to convert into a team. You can get the group ID using the Get-MgGroup cmdlet.


Error Handling

Including error handling with a try-catch block within the script will help you catch errors easily.

try {
    $team = @{
        DisplayName = "Error Handling Team"
        Description = "Team with error handling"
        Visibility = "Private"
        "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
    }

    New-MgTeam -BodyParameter $team
} catch {
    Write-Error "Failed to create team: $_"
}

Cmdlet Tips

  • Using Templates: The template@odata.bind property is crucial for specifying the type of team template to use.
  • Setting Visibility: Choose between "Public" and "Private" visibility based on your organizational needs.
  • Member and Guest Settings: Adjust member and guest settings to control permissions within the team.
  • Fun Settings: Use fun settings to allow or restrict the use of GIFs, stickers, and memes.


Possible Errors & Solutions

Error Cause Solution
Permission Denied You don't have the permission to perform the required action. You need to be a Teams Administrator and have sufficient Graph API permissions like Group.ReadWrite.All.
Invalid Group ID Provided Group ID does not exist or is wrong. Check whether the group ID exists or not using Get-MgGroup cmdlet.
API Rate Limits You have exceeded the API rate limits. Implement API throttling coding logic.


Enhancing the Script

  • Logging: Implement logging to track the success and failure of team creation.
  • User Input: Allow user input for team properties to make the script more interactive.
  • Error Notifications: Send email notifications on script failures to ensure timely intervention.

Frequently Asked Questions

  • What is New-MgTeam used for?
    New-MgTeam is a Microsoft Graph PowerShell cmdlet used to create new Microsoft Teams with specified properties like display name, description, visibility, and settings.
  • How can I create a single Microsoft Team using Graph PowerShell?
    Use the following script to create a team:
    $Body = @{
        displayName = "New Team Name"
        description = "This is a new team."
        visibility = "Private"
        memberSettings = @{
            allowCreateUpdateChannels = $true
        }
    }
    New-MgTeam -BodyParameter $Body
  • Can I create multiple teams using a CSV file?
    Yes, prepare a CSV file with the following format:
    DisplayName,Description,Visibility
    Team 1,Description for Team 1,Private
    Team 2,Description for Team 2,Public
    
    Use this script to process the CSV and create teams:
    $Teams = Import-Csv -Path "C:\Path\To\File.csv"
    foreach ($Team in $Teams) {
            $Body = @{
                displayName = $Team.DisplayName
                description = $Team.Description
                visibility = $Team.Visibility
                }
            New-MgTeam -BodyParameter $Body
    }
        
  • What Graph API permissions are required to create Microsoft Teams?
    You need the Team.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure delegated or application permissions are granted in Azure AD.
💡 You Can Create a New Team or Attach to an Existing Group

New-MgTeam supports both scenarios:
  • Create a brand-new Microsoft Team (which automatically creates a Microsoft 365 group)
  • Attach a Team to an existing group using the group@odata.bind property
To reference an existing group, pass the full URL in this format:
"https://graph.microsoft.com/v1.0/groups('GROUP-ID')"
⚙️ memberSettings, messagingSettings, and funSettings Are Optional But Useful

These properties are not mandatory, but including them helps you customize the team experience — such as who can post messages, allow reactions, or manage channels etc.

If omitted, Microsoft Teams applies default settings for each property.

Conclusion

The New-MgTeam cmdlet is a versatile tool for creating Microsoft Teams with various configurations. Whether creating a simple team, setting specific permissions, or bulk creating teams from a CSV file, this cmdlet provides the flexibility needed to manage teams effectively. By following the examples and tips provided, you can streamline your team creation process and handle potential errors efficiently.

© m365corner.com. All Rights Reserved. Design by HTML Codex