In this article, I am going to explain how to add remove bulk users to Microsoft Teams team or Microsoft 365 group using CLI for Microsoft 365 and PowerShell Script.

What is CLI for Microsoft 365?

CLI for Microsoft 365 is an open-source project driven by the SharePoint Patterns and Practices initiative. The project is built and managed publicly on GitHub and accepts community contributions.

How can contribute to CLI for Microsoft 365 open-source project?

CLI for Microsoft 365 open-source project has a number of feature requests that are a good starting point to contribute to the project.

Why should I contribute to CLI for Microsoft 365?

You may have different individual motivations but working on @office365cli improves coding skills, gain early experience, recognition and chance to work with top class developers around the globe.

How to install the CLI for Microsoft 365?

CLI for Microsoft 365 is distributed as a Node.js package and published on npmjs.com. You can install it using your Node package manager, such as npm or Yarn.

To install the CLI for Microsoft 365, in the command line execute:

npm install -g @pnp/office365-cli

What are all the commands used from CLI for Microsoft 365 in this sample code?

Scenario

Companies pursue to hasten profits growth or enter new marketplace through Mergers and Acquisitions(M&A). M&A typically fails during integration. This also applies to migrating users and data in Microsoft Teams and Groups. Partial acquisition can be pretty tricky. To help make the activity as charming as possible, I have created the following sample script to add/remove bulk users to/from Microsoft Teams team or Microsoft 365 group using CLI for Microsoft 365 commands.

Note: Refactor the code as per your requirement.

Sample Code

Input CSV File Format

humanresource, joseph@domain.com, , remove
finance, joylin@domain.com, owner, add
finance, jeevitha@domain.com, member, add

PowerShell Core

$taskItems = import-csv "sample-input-file.csv" header mailNickname, userEmail, role, action
$groups = m365 aad o365group list -o json | ConvertFrom-Json

ForEach ($taskItem in $taskItems) {

    $mailNickname = $($taskItem.mailNickname)
    $userEmail = $($taskItem.userEmail)
    $role = $($taskItem.role)
    $action = $($taskItem.action)

    $group = $groups | Where-Object { $_.mailNickname -eq "$mailNickname" }
    $user = m365 aad user get --userName $userEmail -o json | ConvertFrom-Json

    Write-Host "Processing: User --> " $user.mail " Group --> " $group.mailNickname

    If ($action -eq "add") {

        If ($role -eq "owner") {
            m365 aad o365group user add --groupId $group.id --userName $user.mail --role Owner;
            Write-Host $user.mail " added as owner in " $group.mailNickname
        }
        ElseIf ($role -eq "member") {
            m365 aad o365group user add --groupId $group.id --userName $user.mail
            Write-Host $user.mail " added as member in " $group.mailNickname
        }
        Else {
            Write-Host "Invalid user role '" $role "'"
        }
    }
    ElseIf ($action -eq "remove") {
        m365 aad o365group user remove --groupId $group.id --userName $user.mail --confirm
        Write-Host $user.mail " removed from " $group.mailNickname
    }
    Else {
        Write-Host "Invalid task action '" $action "'"
    }

}

I hope you find this post helpful.

Photo by Manasa Muthyala