You must use the Command Line Interface for Microsoft 365


Manage the Microsoft Productivity Cloud from your keyboard.

Image: Vector Tradition/Adobe Stock

Administering Microsoft 365 can be complex. Much of what you need to do can be done through its web portal, but there are times when you need something more interactive, using scripts and other tools to create repeatable actions. That’s Command Line Interface for Microsoft 365, a tool that helps you manage Microsoft 365 tenants from the command line and can be used programmatically from PowerShell, bash or of your own choice of scripting environment.

The Command Line Interface for Microsoft 365 was developed by the Microsoft 365 platform community, PnP. Run by both Microsoft staff and Microsoft 365 MVPs, the community was created to share best practices and the tools designed to support them.

SEE: Recruitment Kit: Back-end Developer (TechRepublic Premium)

Providing a tool that brings much of Microsoft 365 management to the command line fits well with their philosophy of not reinventing the wheel, as Microsoft 365 makes it easy to share scripts and queries that can help other admins solve their problems.

Getting Started with the Command Line Interface for Microsoft 365

While most admins are likely to run the CLI from PowerShell on Windows, it’s designed as a node.js JavaScript application and can be installed on Linux or macOS, working with any which supported shell that can call local applications.

Installation is done through npm, using it to manage updates and download beta test builds. It’s also available in a Linux Docker container, ready to run anywhere you have container support. There’s an interesting prospect here of using the CLI as the basis of your own management UI for Microsoft 365, using JavaScript to invoke CLI commands and parse JSON results.

It’s important to remember to regularly check for updates using npm’s tools. The team is adding new features to each release as the Microsoft 365 API surface grows. If you no longer need to use it, another npm call will uninstall the CLI.

The first time you run the CLI, use the m365 login command to authenticate with your tenant. This will walk you through a web-based authorization procedure that defines the permissions needed to manage Microsoft 365 remotely. Once you accept the long list of required permissions, you’ll be taken to your Windows terminal ready to go. Alternatively, you can login with a username and password when interactive login is not an option.

Other features support using Azure Active Directory secrets, a useful option when creating the CLI in your own scripts, although this currently doesn’t work for scripts that need to work with SharePoint Online APIs.

Microsoft 365 Script

Once configured, you can begin managing your Microsoft 365 instance. There are commands for working at different levels within the Microsoft 365 stack, from managing tenant settings to using specific apps such as OneDrive and Teams.

You can use the Command Line Interface for Microsoft 365 to collect information that helps you manage your apps and users. Perhaps more useful is the ability to create complex scripts around these requests, taking parameters from one response and using them as input to another. So you can get a list of all current OneDrive stores, then use that to ask how they’re using your storage budget. You can even automate the emailing of the result to the appropriate managers.

While there are commands for most Microsoft 365 apps and services, it’s no surprise that most CLI commands are meant to work with SharePoint Online. SharePoint remains an important part of the suite and a key tool for collaboration and communication in many organizations. With over 260 commands focused on managing SharePoint, it’s an essential tool for any SharePoint admin. Being able to work with SharePoint programmatically is a huge time saver, and the growing library of commands in the CLI covers much of what you need to do.

There are over 60 commands dedicated to working with Teams. You can dynamically manage apps, users, and channels while being able to automate sending messages to a chat or take data from a script and embed it in an adaptive card for quick interactions . It’s possible to imagine a basic chatbot calling a CLI script and returning the requested data, allowing you to manage your Microsoft 365 tenants without leaving a meeting or from an ongoing team chat.

Filtering and using query results

Commands that return default data using JSON format. This is fine if you are scripting CLI commands and want to use PowerShell’s JSON tools to format the outputs. However, if you’re doing a quick query, it’s not particularly human-readable, so you have the option of choosing text or CSV outputs. The last option is a good option to choose if you get data for a large number of mailboxes, for example, and want to import data into Excel for further analysis or reporting. CSV outputs will allow you to feed data into tools like Power BI to run dashboards and other visualizations.

You can also filter the output using queries to extract the data you want. The query language used is based on the JMESPath JSON tool and owes a lot to familiar regular expression tools. Helpfully, there is a test tool on the JMESPath site that can help you format the query you need to use to get the data you want. In addition to filtering data, you can sort and order it, making it possible to bring relevant results to the top of an answer without having to write your own sort functions.

If you are unsure which command to use, there is support for command completion using the Clink tool for most command-line environments. Alternatively, a single line of PowerShell will add command completion for PowerShell’s own terminal.

Web-based management portals like Microsoft 365 are a useful tool, but if you want to do more than apply basic settings to users or configure apps, a command-line tool like this makes a lot more sense, especially when you can use it to programmatically manage your tenants and users. With cross-platform support, it’s a tool that goes where you are, ensuring it doesn’t get in your way and adapts to your toolchain and workflow. The result is an essential tool for anyone who needs to manage Microsoft 365 and SharePoint Online.


Comments are closed.