Getty Images/iStockphoto

Tip

CLI for Microsoft 365: A guide for administrators

The options to manage the Microsoft 365 platform are seemingly endless, but the CLI for Microsoft 365 offers distinct advantages for admins in the enterprise.

There is no shortage of management options for administrators who work with Azure and Microsoft 365 services.

As IT professionals frequently resort to scripting to manage configurations and automate tasks, the CLI for Microsoft 365 -- formerly known as the Office 365 CLI -- offers a compelling alternative to other methods, such as PowerShell modules and Microsoft Graph. The CLI for Microsoft 365 is a standalone tool that offers several advantages to help a busy IT staff, including cross-platform support, expanded automation capabilities and simplified management of cloud resources. Learn about the setup process then follow the examples to understand how to use the CLI for Microsoft 365 for administrative jobs on the cloud collaboration platform.

Why do admins need a command-line interface?

A CLI offers interaction with a computer program through typed commands in a terminal instead of a GUI. With a CLI, you can automate tasks, perform system administration and run scripts more efficiently than a traditional GUI.

When you use a CLI with Microsoft 365 and Azure to manage cloud services using text-based commands, you get several benefits:

  • Expanded automation capabilities.
  • Scripting interface.
  • Better control over complex tasks.
  • More efficient management and deployment of resources in the cloud.

You can handle most management jobs for Azure and Microsoft 365 in the administration portals. However, some tasks require a script because the functionality does not exist in the Azure or Microsoft 365 administration portals. For example, you must execute commands to set a property within the tenant to limit who can create a team in Microsoft Teams within Microsoft 365. You can see the PowerShell here.

What is the difference between the CLI for Microsoft 365 and PowerShell?

The CLI for Microsoft 365 and PowerShell are command-line interfaces you can use to manage Microsoft 365 and some Azure resources; however, they differ in several ways.

The CLI for Microsoft 365 is a cross-platform command-line interface for managing and automating tasks in Microsoft 365 and some components of Azure. It provides a single unified login to Microsoft 365 workloads.

The CLI for Microsoft 365 gives you the ability to execute scripts and automate everyday administrative tasks, simplifying the management and administration of these services. It is limited in its management scope, but Microsoft updates it frequently.

The CLI is built on top of Node.js and installs on Windows, macOS and Linux.

PowerShell is a command-line interface useful for management of Microsoft 365 and all Azure resources primarily within a Windows OS; however, using PowerShell 7, some modules are cross-platform and work on Windows, macOS and Linux.

PowerShell has an extensive scripting language and automation framework for scripting. Microsoft and the community provide many PowerShell modules to extend its functionality.

How to start using the CLI for Microsoft 365

To use the CLI for Microsoft 365, install Node.js and then the CLI.

Microsoft tested the CLI for Microsoft 365 with Node.js version 6, but the company advises using the latest Long Term Support (LTS) version. If you run Docker containers, then you can also use a Docker image.

Next, launch into Node.js with a console, such as Windows Terminal, and use the npm command to install the CLI for Microsoft 365.

Once installed, execute m365 version to see the currently installed version and if CLI for Microsoft 365 works.

Next, connect to Microsoft 365. The CLI for Microsoft 365 supports several authentication methods, including device code flow, username and password, certificate, secret and standard browser authentication. The default authentication flow uses the device approach. To authenticate this way, type m365 login, and then complete the device flow.

After passing the authentication process, you can execute commands to manage Microsoft 365 services and Microsoft Entra ID, formerly called Azure Active Directory.

For example, to list all users on the Microsoft Entra ID tenant, you can type the following:

# User list output in the default JSON format
m365 aad user list --output text

# User list output as TEXT
m365 aad user list --output csv

# User list output as MARKDOWN
m365 aad user list --output md

# User list output as CSV, displaying only 'DisplayName' and 'Mail' fields
m365 aad user list --properties "displayName,mail" --output csv

# User list output as TEXT, where the 'DisplayName' starts with 'Adele'
m365 aad user list --displayName Adele --output csv

All CLI for Microsoft 365 commands begin with m365 followed by the root object services, such as teams and spo. You then add the specific object, such as team, file and task. You then add actions such as add, get, list, remove or set.

Some further examples are the following:

# Retrieve all planner tasks
m365 planner task list

# Execute the active user detail report for the tenant
m365 tenant report activeuserdetail

# Add a content type
m365 spo contenttype add \
     --webUrl https://domain.sharepoint.com/sites/site \
     --name 'Content Type' –id
0x01007926A26D295BA842B947286090B7F67E \
     --group 'Content Type Group'

# Rename a SharePoint Online site
m365 spo site rename \
     --url https://domain.sharepoint.com/oldsite \
     --newUrl https://domain.sharepoint.com/newsite

# Apply a theme to a SharePoint Online site
m365 spo theme apply \
     --name Theme \
     --webUrl https://domain.sharepoint.com/sites/site \
     --sharePointTheme

# Create a new Team
m365 teams team add \
     --name "Team" \
     --description "Team Description"

How to use the CLI for Microsoft 365 to perform administrative tasks

You can combine commands in the CLI for Microsoft 365 to create scripts to execute single or multiple tasks. Another option is to use PowerShell with the CLI for Microsoft 365.

The advantage is there are no PowerShell modules to load because you use the CLI for Microsoft 365 commands instead. The following PowerShell example removes all Microsoft 365 groups:

$groups = m365 aad o365group list  -o json | ConvertFrom-Json
foreach ($group in $groups)
{
     Write-Host "Deleting: " $group.displayName
     m365 aad o365group remove --id $group.id –confirm
}

The following PowerShell example lists all members of all teams within Microsoft Teams:

$output = @()
$teams = m365 teams team list -o json | ConvertFrom-Json
foreach($team in $teams)
{
     $users = m365 teams user list --teamId $team.id -o json | ConvertFrom-Json
foreach($user in $users)
     {
          $output += [PSCustomObject]@{

               "User ID"=$user.id
               "User Principal Name"=$user.userPrincipalName
               "Assigned Role"=$user.userType
               "Team ID"=$team.id
               "Team Name"=$team.displayName
          }
     }
     $output | Export-Csv `
          -Path "Teams-Members-Report.csv" `
          -NoTypeInformation
}

Combining the power of the CLI for Microsoft 365 with PowerShell creates a powerful scripting approach that helps you, as an IT administrator, perform tasks easier and faster.

Liam Cleary runs his own consulting company that helps customers work with Microsoft 365 and Azure-based technologies. He specializes in internal and external collaboration, document and records management, business process automation and security implementation.

Next Steps

An explanation of CLI, GUI and NUI

Dig Deeper on IT operations and infrastructure management

Cloud Computing
Search Enterprise Desktop
Search Virtual Desktop
Close