Getting Started With Office 365 CLI

Microsoft keeps on giving surprises to developers by adding more tools/services on the cloud front, especially in Office 365. Recently, a new open source cross-platform service called Office 365 was introduced, to run commands from any platform against SharePoint Online.

We have PowerShell commands for SharePoint Online only from Windows OS. To avoid this limitation, Office 365 CLI was introduced to work in all platforms.

Office 365 CLI helps us to manage the Office 365 tenant across all platforms.  It enables us to use any command line tools in all operating systems to manage the objects in the Office 365 tenant with the help of Nodejs and NPM package.

Install Office 365 CLI to the local machine

We require a Nodejs with NPM in the machine, when we are trying to use Office 365 ClI commands.

Use the below npm command to install the Office365 CLI globally,

npm install -global @pnp/office365-cli

Office 365 CLI

Office 365 Command Lists

After installing the office365-cli, we have to use office365 or o365 command to start the Office365 CLI environment. This command navigates us to separate command lines to run the commands from Office365CLI.

Office 365 CLI

Now, we are inside of Office365CLI and ready to run the commands to manage the Office 365 tenant.

Enter the help command in the command line. This shows the installed version of Office 365 CLI, the syntax to use, help with any specific commands, and the commands associated with each group, also called Command Groups.

At present, we have two high level command groups, aad and spo, and each contains 8 and 64 sub commands respectively.

Office 365 CLI

The commands under aad are used to connect Azure Active Directory Graph and retrieve the information from Azure Active Directory.

Office 365 CLI

The commands under spo are used to connect SharePoint Online and fetch information from sites and other objects

Office 365 CLI

Each sub command from spo also has a multiple sub command within it to get and set from SharePoint online site.

Connect SharePoint Online Site

Follow the below steps to connect the SharePoint online site to run the commands to access SharePoint object like, list, app, site, etc..

  1. After installing the @pnp/Office365-cli command enter the below command.

    spo connect

    Office 365 CLI

    It returns the code to authenticate your device with SharePoint online. This helps to run the other commands by using this authentication.
  1. Copy the URL and code mentioned after the connect command

  2. Navigate to the above URL in browser and Enter the code to authenticate the command line to connect with SharePoint online

    Office 365 CLI

  3. After entering the code, the page asks you permission to connect SharePoint online with Microsoft SharePoint Online Management Shell. Click Continue button

    Office 365 CLI

  4. You will redirect to the login page. Enter your organization credentials. If you successfully login, it will redirect you to a successful page.

    Office 365 CLI

  5. Close the browser and navigate to command line.

    Office 365 CLI

Now, we have successfully connected to SharePoint Online and are ready to use other office365cli commands to access SharePoint.

Enter the below command to get all available lists from the given url from the connected tenant.

spo list list –u

The output looks like below,

Office 365 CLI


We have learned  about Office365 CLI and how to use it to connect SharePoint online and retrieve the information from the site.

Similar Articles