Episode #149​ – Introducing Microsoft 365 CLI

Here you can find the transcript of Episode #149 of PiaSys TechBites.

Welcome back to PiaSys Tech Bites. As I promised to my friend Waldek last week, today I want to give you a brief introduction about the Microsoft 365 CLI tool. In fact, the Microsoft 365 CLI is a command-line interface, which is cross-platform. So you can use it on Windows, Linux, macOS. You can use it in any kind of shell like Bash, Azure Cloud Shell, PowerShell, of course, and Cmder and so on and so forth. Technically speaking, it is a CLI, to manage most of the main workloads that are available in Microsoft 365. Like for example, SharePoint Online, Microsoft Teams, Microsoft To Do, and so on and so forth. Plenty of workloads that you can manage using the CLI approach. It is an open source project. And here, you can see the URL of the GitHub repository, and this is a really brilliant and wonderful community effort.

Because lot of people from the community are contributing and building this great CLI tool. Last but not least, you can find a bunch of samples still on GitHub from where you can take inspiration to create your own script based on the Microsoft 365 CLI. So let me move to the demo environment and let me show you how to start using Microsoft 365 CLI. So in order to play with the Microsoft 365 CLI, you’ll need any shell of your choice. And for example, here I’m using PowerShell. And first of all, you will need to install the Microsoft 365 CLI using “npm install -g @pnp/cli-microsoft365” to make it global. Of course, I already installed it on my environment and so, I can simply start using it. So, first of all, we need to log in to the target environment.

The first option is to just say, Microsoft 365 login. You will be prompt with the URL for the device login. So if you copy this value, which will be the device code and you control click on this link, and you will be able to provide the code and to login with your credentials into your target tenant. By default, a Microsoft 365 CLI we’ll use the PnP Management Shell, a multi-tenant Azure AD application that we have registered in the Microsoft 365 PnP tenant. So you will have a restricted set of permissions, delegated permissions, for your user. Now, if you want to play with it, you can. For example, use the Microsoft 365 CLI and provide in the command that you want to use. Let’s say that you want to connect to a site like this one, for example, and you want to see what the list of lists in this site is.

So, first of all, let me use the M365 spo so that we target SharePoint Online. We target a site in SharePoint Online, and we want to get that specific site and I provide the URL of the target site. By doing that, we can get back the information about the site, which can also be retrieved using the “–output JSON” to see the output as a JSON file instead of a table with all of the results. Imagine that you want to manage the JSON and do stuff on top of the returned file. Moreover, if you want to get the list of lists in the target site, you can easily use the following command. So, M365 spo still, then list because you want to access the collection of lists. And you want to list all of the lists in the site.

So you provide the URL of the site and you will get back the collection of lists, again as a table, or you can eventually get it as a JSON object, but you can also do more. For example, in my Azure Active Directory tenant, I registered a custom application. So under Azure ID, I went to app registrations and I registered a new application. Let me search for it right now. M365 CLI and following the instruction that you can see in the link just below this video in YouTube, you can create your own application. You can register your own application and you can use it to access and to authenticate when you log in with Microsoft 365 CLI. By doing that, you will be able to configure your custom permissions. So for example, in my application, these are the permissions that I configured.

So I can go back, I can logout from my Microsoft 365 CLI session, and I can login back using the application ID and the tenant ID of the application that I registered in my Azure AD tenant. So let me do that. Let me do Microsoft 365 login. I will provide the app ID, which will be the unique ID of my application, so this value. And the directory, this, so the tenant ID of my target tenant. By doing that, again, I will have the device login. So let me copy the device code and let me go to the device login URL. I paste the code. I will confirm my user account. And now, I will be authenticated using the Microsoft 365 CLI application that I registered in my tenant. And again, I can use it. For example, to read the list of teams that I have in Microsoft Teams in my tenant.

And as you can see here, I have a bunch of teams available. But I can also use it to get the channels that I have in a specific team. Like for example, I can do something like that. M365 teams, I target the channel and I want to get the list of channels in the team with this specific ID. And by doing that, I can get back the channels in my target team. Of course, we can also use an application which will have permissions to create objects. And we can use M365 CLI to create new teams, to create new channels, to register applications in Azure Active Directory and stuff like that. Moreover, as I told you, the Microsoft 365 CLI is a cross platform solution. So we can also use, for example, in a Ubuntu system running in my windows machine, we can use Docker image that we can use to run Microsoft 365 CLI inside my Ubuntu virtual machine.

So let me do that, Docker run, and now I can play still inside my Ubuntu machine, virtual machine, using the Microsoft 365 CLI. So again, M365, I can see the help for the commands of Microsoft 365, and I can say Microsoft 365 login. And I will be prompt again to login, providing a device code as like as before. And I can control click on this one. I provide the code. And that’s it. I am again using M365 CLI inside a Linux machine. So really simple and easy. Like always, thank you for watching this video. I hope you found it interesting, and I’m really looking forward to seeing you next week. And remember, subscribe to this channel. Thank you.