Manage the Microsoft productivity cloud from your keyboard.
administer Microsoft 365 can be complicated. Much of what you need to do can be done through the web portal, but there are times when you need something more interactive, using scripts and other tools to build repeatable actions. That is what the command line interface for Microsoft 365 is, a tool that helps you manage Microsoft 365 tenants from the command line and can be used programmatically from PowerShell, bash, or your own scripting environment.
The CLI for Microsoft 365 was developed by the Microsoft 365 Platform Community, PnP. The community, run by both Microsoft employees and Microsoft 365 MVPs, is set up to share best practices and tools designed to support them.
TO SEE: Hiring Kit: Back-end Developer (Tech Republic Premium)
Providing 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 to 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. When you no longer need it, another npm call will remove the CLI.
The first time you run the CLI, use the m365 login command to authenticate with your tenant. This walks you through a web-based authorization process that sets up the permissions needed to manage Microsoft 365 remotely. Once you accept the long list of required permissions, you will be returned to your Windows Terminal, ready to use. Alternatively, you can log in with a username and password if interactive login is not an option.
Other functions support the use of Azure Active Directory secrets, a useful option when building the CLI into your own scripts, although this doesn’t currently work for scripts that need to work against Share point APIs from online.
Scripting Microsoft 365
Once set up, you can start managing your Microsoft 365 instance. There are commands to work at different levels within the Microsoft 365 stack, from managing tenant settings to working with specific apps such as A disc and teams.
You can use the CLI for Microsoft 365 to collect information that helps you manage your applications and users. Perhaps even more useful is the ability to build complex scripts around these queries, taking parameters from one answer and using them as input in another. So you can get a list of all current OneDrive stores and then use that to ask how they are using your storage budget. You could even automate emailing that result to the right 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 an important tool for collaboration and communication in many organizations. With over 260 commands focused on managing SharePoint, it is an essential tool for any SharePoint administrator. Being able to work programmatically with SharePoint saves a lot of time and the growing library of commands in the CLI covers much of what you need to do.
There are over 60 assignments dedicated to working with Teams. You can dynamically manage apps, users, and channels, while also automating sending messages to chat or extracting data from a script and embedding it in an adaptive map for quick interactions. It is possible to imagine a simple chatbot that calls a CLI script and returns requested data, so you can manage your Microsoft 365 tenants without leaving a meeting or from an ongoing team chat.
Filter and use search results
Commands that return standard data using the JSON format. This is fine if you build CLI commands in scripts and want to use PowerShell’s JSON tooling to format output. However, if you do a quick search, it won’t be particularly human readable, so you have the option to choose text or CSV output. The latter option is a good choice if you are getting data for a large number of, for example, mailboxes, and you want to import data into Excel for further analysis or reports. CSV output lets you enter data into tools like Power BI to run dashboards and other visualizations.
You can also filter the output by using queries to extract the data you want. The search language used is based on: the JMESPath JSON tool and owes a lot to the well-known regular expression tools. Conveniently, there is a testing 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, you can sort and organize data so that relevant results appear at the top of a response without having to write your own sorting functions.
If you’re not quite sure which command to use, there’s a quest completion support using the Clink tooling for most command line environments. Alternatively, a single line of PowerShell adds command completion for PowerShell’s own terminal.
Web-based administration portals like Microsoft 365s are a useful tool, but if you want to do more than apply basic settings to users or configure applications, a command-line tool like this makes much 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 everywhere, makes sure it doesn’t get in your way and fits your toolchain and workflow. The result is an essential tool for anyone who needs to manage Microsoft 365 and SharePoint Online.