Manage the Microsoft productivity cloud from your keyboard.
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 build repeatable actions. That’s what the command-line interface for Microsoft 365 is, a tool that helps you manage Microsoft 365 tenants from the command line, and which can be used programmatically from PowerShell, bash or your own choice of scripting environment.
The CLI for Microsoft 365 has been developed by the Microsoft 365 Platform Community, PnP. Run by both Microsoft staff and Microsoft 365 MVPs, the community was set up to share best practices and tools designed to support them.
LAKE: Hiring kit: Backend Developer (TechRepublic Premium)
Delivering a tool that brings much of Microsoft 365 management into the command line fits well with their ethos 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 CLI for Microsoft 365
It’s important to remember to check for updates regularly using npm’s tools. The team adds new features with each release as the Microsoft 365 API surface grows. If you don’t need to use it anymore, another npm call will uninstall the CLI.
The first time you run the CLI, use the m365 login command to authenticate against your tenant. This will take you through a web-based authorization procedure that sets the permissions needed to manage Microsoft 365 remotely. Once you accept the long list of required permissions, you’ll be dropped back into your Windows Terminal ready to go. Alternatively, you can log in with a username and password when interactive login isn’t an option.
Other features support using Azure Active Directory secrets, a useful option when building the CLI into your own scripts, though this currently won’t work for any scripts that need to work against SharePoint Online’s APIs.
Microsoft 365 scripting
Once set up, you can start to manage your Microsoft 365 instance. There are commands for working at different levels within the Microsoft 365 stack, from managing tenant settings to working with specific apps like OneDrive and Teams.
You can use the CLI for Microsoft 365 to collect information that helps you manage your applications and users. What’s perhaps more useful is the ability to build complex scripts around these queries, taking in parameters from one response and using them as inputs into another. So you can get a list of all current OneDrive stores, then use that to query how they’re using your storage budget. You could even automate emailing that result to the appropriate managers.
While there are commands for most Microsoft 365 applications and services, it’s not surprising that most of the commands in the CLI are for working 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 big time saver, and the growing library of commands in the CLI cover much of what you need to do.
There’s over 60 commands dedicated to working with teams. You can dynamically manage apps, users and channels while also 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 requested data, allowing you to manage your Microsoft 365 tenants without leaving a meeting or from inside an ongoing team chat.
Filtering and using query results
Commands that return data default to using the JSON format. This is fine if you’re building CLI commands into scripts and want to use PowerShell’s JSON tooling to format outputs. However, if you’re making 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 one to choose if you’re getting data for a large estate of, say, mailboxes, and you want to import data into Excel for further analyzes or reports. CSV outputs will allow you to feed data into tools like Power BI to run dashboards and other visualizations.
You’re able to filter output too by 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. Usefully, there’s a test tool on the JMESPath site that can help you format the query you need to use to get the data you want. As well as filtering data, you’re able to sort and order it, helping bring relevant results to the top of a response without having to write your own sorting functions.
If you’re not quite sure what command to use, there’s support for command completion using the Clink tooling 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’s are a useful tool, but if you want to do more than apply basic settings to users or configure applications, then 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 that it doesn’t get in your way and fits with your toolchain and workflow. The result is an essential tool for anyone who has to manage Microsoft 365 and SharePoint Online.