Microsoft m365-cli#
Installation#
Install npm using:
npm install -g @pnp/cli-microsoft365
You may need to set the path:
vi ~/.bashrc
Add the following to the bottom
if [ -d "/usr/local/bin" ] ; then
PATH="$PATH:/usr/local/bin"
fi
source ~/.bashrc
Login#
Note
These instructions are for a remote headless server without a browser.
Run the following command
m365 setup --debug
You will get a url. Note the port number and then run a new ssh terminal forwarding the port to your local machine:
Note
In my example the port is 37651, howeever it is a random port each time
Forward your local port with:
ssh root@<yourserver> -L 37651:localhost:37651
Now copy the full link and paste it in your local browser to authenticate.
Confirm credentials#
Run:
m365 status
Create a Microsoft Teams Group#
Create a group#
Run the following:
m365 entra m365group add

See the groups details#
m365 entra m365group get
Now type in the groups name
Add users to a Teams group#
Note
You get the groupID from the step above Replace yourname.yoursurname@yourdomain.com with the users details
m365 entra m365group user add --groupId 100000aa-ea12-4b123-9a123-f1111111 --userNames "yourname.yoursurname@yourdomain.com" --role member
Batch add users to a Teams group via csv#
These instructions are for batch adding users from a csv file to your teams group on a Linux machine
Install powershell on your Linux box
Note
This is for a Red hat based server
dnf install https://github.com/PowerShell/PowerShell/releases/download/v7.5.1/powershell-7.5.1-1.rh.x86_64.rpm
Create a csv file with the following details:
vi teams.csv
Note
Include the header UPN,teamName,teamId,role
Warning
UPN,teamName,teamId,role
name1.surname1@yourdomain.com,Teams_name,100000aa-ea12-4b123-9a123-f1111111,owner
name2.surname2@yourdomain.com,Teams_name,100000aa-ea12-4b123-9a123-f1111111,member
Now create a MicroSoft BAT script:
vi script.bat
Add
Note
Make sure the location of your csv file is referenced.
#Check if connected to M365
$m365Status = /usr/local/bin/m365 status --output text
if ($m365Status -eq "Logged Out") {
/usr/local/bin/m365 login
}
#Import csv
$usersCsvFile = Import-Csv -Path "/root/teams.csv"
#Add users to the Team
foreach ($row in $usersCsvFile) {
Write-Host "Adding $($row.UPN) to the $($row.teamName) Team" -ForegroundColor Magenta
/usr/local/bin/m365 entra m365group user add --groupId $row.teamId --userNames "$($row.UPN)" --role $row.role
}
Now run the script to to add the users from your csv file to your teams group:
pwsh script.bat
View the group in Microsoft Teams#
Open teams, click on the chats and scroll to the bottom.
Click on "See all your teams"
Click on "Teams you own" and you should see it in the drop down