Microsoft Graph CLI provides convenient methods to access Microsoft Graph capabilities using a simplistic command line interface experience on any operating system and any shell.
A command line tool should work on any terminal. We recommend:
- Windows Terminal + version
- MacOS (Minimum version: 10.12 Sierra) terminal
- PowerShell version 7.3.2
To download the CLI to get started for Windows, Mac, or Linux. here on the assets link of the latest release: CLI Download
- Extract downloaded CLI tool for Windows to folder
- Execute program using mgc (on Windows CMD) or ./mgc (on Windows PowerShell) command.
To allow executing the mgc command without prefixing it with ./
, you can add the executable's directory to the OS Path environment variable.
On Windows PowerShell, you can add the current directory to the path by running:
$env:Path += ";$(pwd)"
On CMD, the command becomes:
set PATH=%PATH%;%cd%
Though adding the current directory to the path doesn't change how you run mgc on CMD, it allows you to switch the directory you're working in. This is useful for when you need to use a different directory to save files downloaded from the CLI.
The commands above will update your Path for the current command session, and will need to be run whenever you start a new session e.g. when opening a new window. To make the changes permanent, you can update the environment variables by using the System properties dialog. You can find it by either using windows search (it appears as view advanced system settings) in the start menu, or following the steps below:
- Open the Settings app
- Click on System
- Select the About option. It should be the last one in the list.
- In the About screen, locate and click on the Advanced system settings link in the Related links section just below the device specifications.
- Once the dialog is open, click on the Environment Variables button on the bottom right.
- Select the Path environment variable and click Edit.
- Add the directory with the CLI to the path here.
The Environment Variables screen should have 2 sections that both have a Path entry. The User variables section adds to the current user's path and other users' environments should remain unchanged. The System variables section will make the changes for all users in the computer.
- Download the CLI archive for your Mac.
- If you have an M1 Mac computer, download the
msgraph-cli-osx-arm64...
package. - If you have an Intel processor, download the
msgraph-cli-osx-x64...
package.
- If you have an M1 Mac computer, download the
- Extract downloaded CLI tool for Mac to a folder.
- Open a terminal in the new folder
- Execute the program using ./mgc command.
If you'd like to run the CLI from any folder, you need to either update the PATH environment variable for your shell, or add a symbolic link to the CLI into a location in the PATH (e.g. /usr/bin). Adding a symbolic link will need super user rights depending on the location you choose.
Adding to the PATH for your current session is also an option as it is in Windows. To do this, run the following command from your terminal in the directory of the CLI:
export PATH=$PATH:$(pwd)
Adding the location permanently can be done for the current user by editing your ~/.bash-profile
file.
Add the following line to your profile:
export PATH=$PATH:[path-to-the-cli-folder]
Adding a symbolic link into /usr/bin
(not recommended) can be done with the command below:
sudo ln -s [path-to-the-cli-folder]/mgc /usr/bin
Download installation package from your browser OR via Curl
curl -LO <link> && tar -xzf <filename>
Note: For headless Linux environments, there's an additional step to configure the keyring for access token encryption.
To set up the PATH environment variable for your current session, run the following command from your terminal in the directory of the CLI:
export PATH=$PATH:$(PWD)
Adding the location permanently can be done for the current user by editing your shell's profile file
(~/.bash-profile
or ~/.zsh-profile
) file. Add the following line to your profile:
export PATH=$PATH:[path-to-the-cli-folder]
Adding a symbolic link into /usr/bin
(not recommended) can be done with the command below:
sudo ln -s [path-to-the-cli-folder]/mgc /usr/bin
Required if your Linux distribution does not have a desktop environment e.g. WSL, Docker
Note: Not setting this up for this environment will result in authentication failures.
Required packages:
- systemd (ensure the dbus service is properly set up)
- dbus
- gnome-keyring
On WSL, systemd can be enabled by following these instructions
- Install gnome-keyring
sudo apt-get install --assume-yes gnome-keyring
- If running in a headless environment(e.g. WSL), you may need to run the following commands as well to start gnome-keyring in your shell session:
export KEYRING_PASSWORD=any-password
printf '%s' "$KEYRING_PASSWORD" | gnome-keyring-daemon --daemonize --components=secrets --unlock
- Run mgc
mgc login
Find docker instructions in the docker folder here. See the shell script used for docker in this repository
- Delete the CLI installation folder
- If path enviroment variable had been set, remove it.
Note: this step is NOT required unless your client will be calling APIs that are protected by the Microsoft Identity Platform like Microsoft Graph OR using Client Certificate authentication option.
Follow the instructions in Register an application for Microsoft identity platform authentication to get an application ID (also know as a client ID).
Replace YOUR_CLIENT_ID
with the client ID from your app registration.
Navigate to your download folder and use the mgc (on Windows/Mac) or ./mgc (on WSL or Linux) command to run commands on the CLI.
Add -h to your commands to view additional commands.
CLI supports both delegeted and app-only authentication strategies. Run the command below to see supported authentication strategies.
#View supported authentication strategies. Default is Device Code
mgc login --help
OR
mgc login --client-id `YOUR_CLIENT_ID` --tenant-id `YOUR_TENANT_ID` --scopes User.ReadWrite --scopes Mail.ReadWrite
1. DeviceCode authentication strategy
#Using the Default authentication (which is Device Code)
mgc login
OR
#Using the DeviceCode authentication explicitly
mgc login --strategy DeviceCode
2.Interactive Browser authentication strategy
# Using interactive Browser authentication.
mgc login --strategy InteractiveBrowser
1. Client Certificate authentication strategy
Note: Registering an application is required when using Client Certificate
# Using Client Certificate authentication.
mgc login --strategy ClientCertificate
Add -h to your command to view information that contains documentation links with permissions
You can access the your user's details using:
mgc me get
This will also display your User ID which is required for many queries involving your user (the "me" concept has been removed from Microsoft Graph CLI).
Qn: Why am I not getting a response back after running a command?
Ans:add --debug to your command
You can find additional samples here (contains sample commands for CMD, PowerShell & Bash) CLI samples
Note: This repository is for the .NET CLI. If you are looking for the Python CLI, it's been moved to the msgraph-cli-archived repository