Bulk add/remove users to Microsoft Teams using the CLI for Microsoft 365

Published Oct 05 2021 03:08 AM 1,900 Views
New Contributor

In our last post we saw how to Export conversations from Microsoft Teams Channels using the CLI for Microsoft 365.

We will continue our journey with the most popular sample scripts created  for the CLI for Microsoft 365. This time we will show you how to add and remove users to Microsoft Teams in bulk. The original script was created by our community member Joseph Velliah.

 

The script requires a CSV file with different parameters:

- groupMailNickname: the mail alias used for the Microsoft 365 Group associated to the current team

- userPrincipalName: the UPN of the user that should be processed

- role (owner, member): the role of the user. Can be owner or member. Keep it empty if you want to remove a user

- operation (add, remove): operation to execute

 

groupMailNickname1, user1@domainname.com, owner, add
groupMailNickname2, user2@domainname.com, member, add
groupMailNickname3, user3@domainname.com, , remove

 

 

The script in question looks as follows:

 

$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 "'"
    }
}

 

 

You can find this and many more samples on how to use the CLI for Microsoft 365 in our official samples section.

 

What is the CLI for Microsoft 365?

CLI for Microsoft 365 is a cross-platform CLI that allows you to manage various configuration settings of Microsoft 365 and SharePoint Framework projects no matter which operating system or shell you use.


While building solutions for Microsoft 365 expands beyond the Windows operating system, managing many of the platform settings is possible only through PowerShell on Windows. As more and more users work on non-Windows machines, it’s inconvenient for them to have to use a Windows virtual machine to configure their tenants. With the CLI for Microsoft 365, you can configure your tenant no matter which operating system you use. Additionally, using CLI for Microsoft 365, you can manage your SharePoint Framework projects.

 

Try it today

Get the latest release of the CLI for Microsoft 365 from npm by executing:

 

npm i -g @pnp/cli-microsoft365

 

If you need more help getting started or want more details about the commands, the architecture or the project, go to aka.ms/cli-m365.

If you see any room for improvement, please, don’t hesitate to reach out to us either on GitHub or twitter.

%3CLINGO-SUB%20id%3D%22lingo-sub-2799296%22%20slang%3D%22en-US%22%3EBulk%20add%2Fremove%20users%20to%20Microsoft%20Teams%20using%20the%20CLI%20for%20Microsoft%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2799296%22%20slang%3D%22en-US%22%3E%3CP%20data-unlink%3D%22true%22%3EIn%20our%20last%20post%20we%20saw%20how%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmicrosoft-365-pnp-blog%2Fexport-conversations-from-microsoft-teams-channels-using-the-cli%2Fba-p%2F2762619%22%20target%3D%22_self%22%3EExport%20conversations%20from%20Microsoft%20Teams%20Channels%20using%20the%20CLI%20for%20Microsoft%20365.%3C%2FA%3E%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3EWe%20will%20continue%20our%20journey%20with%20the%20most%20popular%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2Fsample-scripts%2Faad%2Fmanage-group-users%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Esample%20scripts%3C%2FA%3E%20created%26nbsp%3B%20for%20the%20CLI%20for%20Microsoft%20365.%20This%20time%20we%20will%20show%20you%20how%20to%20add%20and%20remove%20users%20to%20Microsoft%20Teams%20in%20bulk.%20The%20original%20script%20was%20created%20by%20our%20community%20member%20%3CA%20href%3D%22https%3A%2F%2Fblog.josephvelliah.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EJoseph%20Velliah.%3C%2FA%3E%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20requires%20a%20CSV%20file%20with%20different%20parameters%3A%3C%2FP%3E%0A%3CP%3E-%20groupMailNickname%3A%20the%20mail%20alias%20used%20for%20the%20Microsoft%20365%20Group%20associated%20to%20the%20current%20team%3C%2FP%3E%0A%3CP%3E-%20userPrincipalName%3A%20the%20UPN%20of%20the%20user%20that%20should%20be%20processed%3C%2FP%3E%0A%3CP%3E-%20role%20(owner%2C%20member)%3A%20the%20role%20of%20the%20user.%20Can%20be%20owner%20or%20member.%20Keep%20it%20empty%20if%20you%20want%20to%20remove%20a%20user%3C%2FP%3E%0A%3CP%3E-%20operation%20(add%2C%20remove)%3A%20operation%20to%20execute%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EgroupMailNickname1%2C%20user1%40domainname.com%2C%20owner%2C%20add%0AgroupMailNickname2%2C%20user2%40domainname.com%2C%20member%2C%20add%0AgroupMailNickname3%2C%20user3%40domainname.com%2C%20%2C%20remove%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20in%20question%20looks%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24taskItems%20%3D%20import-csv%20%22sample-input-file.csv%22%20%E2%80%93header%20mailNickname%2C%20userEmail%2C%20role%2C%20action%0A%24groups%20%3D%20m365%20aad%20o365group%20list%20-o%20json%20%7C%20ConvertFrom-Json%0A%0AForEach%20(%24taskItem%20in%20%24taskItems)%20%7B%0A%0A%20%20%20%20%24mailNickname%20%3D%20%24(%24taskItem.mailNickname)%0A%20%20%20%20%24userEmail%20%3D%20%24(%24taskItem.userEmail)%0A%20%20%20%20%24role%20%3D%20%24(%24taskItem.role)%0A%20%20%20%20%24action%20%3D%20%24(%24taskItem.action)%0A%0A%20%20%20%20%24group%20%3D%20%24groups%20%7C%20Where-Object%20%7B%20%24_.mailNickname%20-eq%20%22%24mailNickname%22%20%7D%0A%20%20%20%20%24user%20%3D%20m365%20aad%20user%20get%20--userName%20%24userEmail%20-o%20json%20%7C%20ConvertFrom-Json%0A%0A%20%20%20%20Write-Host%20%22Processing%3A%20User%20--%26gt%3B%20%22%20%24user.mail%20%22%20Group%20--%26gt%3B%20%22%20%24group.mailNickname%0A%0A%20%20%20%20If%20(%24action%20-eq%20%22add%22)%20%7B%0A%0A%20%20%20%20%20%20%20%20If%20(%24role%20-eq%20%22owner%22)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20m365%20aad%20o365group%20user%20add%20--groupId%20%24group.id%20--userName%20%24user.mail%20--role%20Owner%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20%24user.mail%20%22%20added%20as%20owner%20in%20%22%20%24group.mailNickname%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20ElseIf%20(%24role%20-eq%20%22member%22)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20m365%20aad%20o365group%20user%20add%20--groupId%20%24group.id%20--userName%20%24user.mail%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20%24user.mail%20%22%20added%20as%20member%20in%20%22%20%24group.mailNickname%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20Else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20%22Invalid%20user%20role%20'%22%20%24role%20%22'%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20ElseIf%20(%24action%20-eq%20%22remove%22)%20%7B%0A%20%20%20%20%20%20%20%20m365%20aad%20o365group%20user%20remove%20--groupId%20%24group.id%20--userName%20%24user.mail%20--confirm%0A%20%20%20%20%20%20%20%20Write-Host%20%24user.mail%20%22%20removed%20from%20%22%20%24group.mailNickname%0A%20%20%20%20%7D%0A%20%20%20%20Else%20%7B%0A%20%20%20%20%20%20%20%20Write-Host%20%22Invalid%20task%20action%20'%22%20%24action%20%22'%22%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20find%20this%20and%20many%20more%20samples%20on%20how%20to%20use%20the%20CLI%20for%20Microsoft%20365%20in%20our%20%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2Fsample-scripts%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Eofficial%20samples%20section.%3C%2FA%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--348605782%22%20id%3D%22toc-hId--327573338%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-2138907051%22%20id%3D%22toc-hId--2135027801%22%3EWhat%20is%20the%20CLI%20for%20Microsoft%20365%3F%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3ECLI%20for%20Microsoft%20365%3C%2FA%3E%20is%20a%20cross-platform%20CLI%20that%20allows%20you%20to%20manage%20various%20configuration%20settings%20of%20Microsoft%20365%20and%20SharePoint%20Framework%20projects%20no%20matter%20which%20operating%20system%20or%20shell%20you%20use.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EWhile%20building%20solutions%20for%20Microsoft%20365%20expands%20beyond%20the%20Windows%20operating%20system%2C%20managing%20many%20of%20the%20platform%20settings%20is%20possible%20only%20through%20PowerShell%20on%20Windows.%20As%20more%20and%20more%20users%20work%20on%20non-Windows%20machines%2C%20it%E2%80%99s%20inconvenient%20for%20them%20to%20have%20to%20use%20a%20Windows%20virtual%20machine%20to%20configure%20their%20tenants.%20With%20the%20CLI%20for%20Microsoft%20365%2C%20you%20can%20configure%20your%20tenant%20no%20matter%20which%20operating%20system%20you%20use.%20Additionally%2C%20using%20CLI%20for%20Microsoft%20365%2C%20you%20can%20manage%20your%20SharePoint%20Framework%20projects.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1465498771%22%20id%3D%22toc-hId--1444466327%22%3ETry%20it%20today%3C%2FH3%3E%0A%3CP%3EGet%20the%20latest%20release%20of%20the%20CLI%20for%20Microsoft%20365%20from%20npm%20by%20executing%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Enpm%20i%20-g%20%40pnp%2Fcli-microsoft365%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20need%20more%20help%20getting%20started%20or%20want%20more%20details%20about%20the%20commands%2C%20the%20architecture%20or%20the%20project%2C%20go%20to%20%3CA%20href%3D%22http%3A%2F%2Faka.ms%2Fcli-m365%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Eaka.ms%2Fcli-m365%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EIf%20you%20see%20any%20room%20for%20improvement%2C%20please%2C%20don%E2%80%99t%20hesitate%20to%20reach%20out%20to%20us%20either%20on%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpnp%2Fcli-microsoft365%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EGitHub%3C%2FA%3E%20or%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fclimicrosoft365%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Etwitter%3C%2FA%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2799296%22%20slang%3D%22en-US%22%3E%3CP%20data-unlink%3D%22true%22%3EIn%20our%20last%20post%20we%20saw%20how%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmicrosoft-365-pnp-blog%2Fexport-conversations-from-microsoft-teams-channels-using-the-cli%2Fba-p%2F2762619%22%20target%3D%22_self%22%3EExport%20conversations%20from%20Microsoft%20Teams%20Channels%20using%20the%20CLI%20for%20Microsoft%20365.%3C%2FA%3E%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3EWe%20will%20continue%20our%20journey%20with%20the%20most%20popular%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2Fsample-scripts%2Faad%2Fmanage-group-users%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Esample%20scripts%3C%2FA%3E%20created%26nbsp%3B%20for%20the%20CLI%20for%20Microsoft%20365.%20This%20time%20we%20will%20show%20you%20how%20to%20add%20and%20remove%20users%20to%20Microsoft%20Teams%20in%20bulk.%20The%20original%20script%20was%20created%20by%20our%20community%20member%20%3CA%20href%3D%22https%3A%2F%2Fblog.josephvelliah.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EJoseph%20Velliah.%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2799296%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EGetting%20started%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Sep 30 2021 06:54 AM
Updated by: