Skip to content

Instantly share code, notes, and snippets.

@petergs
Last active April 25, 2025 20:31
Show Gist options
  • Select an option

  • Save petergs/38369141f9683cd762a5afb8bc229a58 to your computer and use it in GitHub Desktop.

Select an option

Save petergs/38369141f9683cd762a5afb8bc229a58 to your computer and use it in GitHub Desktop.
Microsoft Graph CLI Incantations

Microsoft Graph CLI Cheatsheet

I've generally found the Microsoft Graph CLI (mgc) hard to work with. In many cases, harder than using the Graph API endpoints directly or language-specific SDKs. In general, Entra directory-related commands seem to work well, but other parts of the Graph API are rough around the edges from a usability perspective.

This doc provides some quick examples so I never have to work through the trial-and-error of figuring them out again.

OneDrive

Listing and downloading items

# store the drive id of the current user's onedrive
drive_id=$(mgc users drives list --user-id me | jq -r '.value.[] | select(.name == "OneDrive") | .id')

# or a simpler option might be
drive_id=$(mgc users drive get --user-id me)

# list drive items in the top-level folder (root)
mgc drives items children list --drive-id $drive_id --drive-item-id root

# or for a cleaned-up list, try this jq
mgc drives items children list --drive-id $drive_id --drive-item-id root | jq '[.value.[] | {"id": .id, "name": .name, "type": (if .folder then "folder" elif .file then "file" else null end)}]'

# download a file
mgc drives items content get --drive-id $drive_id --drive-item-id <id> --output-file <filename>

Issues

A set of issues I've ran into or would be interested in seeing implemented:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment