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

What is Office 365 CLI?

Office 365 CLI 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 Office 365 CLI open-source project?

Office 365 CLI 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 Office 365 CLI?

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 Office 365 CLI?

Office 365 CLI 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 Office 365 CLI, in the command line execute:

npm install -g @pnp/office365-cli

What are all the commands used from Office 365 CLI 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 Office 365 CLI 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 = o365 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 = o365 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") {
            o365 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") {
            o365 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") {
        o365 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. Let me know your thoughts in the comments section.

Photo by Manasa Muthyala