M365 Work Skill (m365-cli)
Manage a Microsoft 365 work/school account via the m365 CLI.
Use --json for structured output suitable for AI agent consumption.
Prerequisites
- - Node.js 18+
- INLINECODE2 installed globally: INLINECODE3
- Authenticated:
m365 login (work/school is the default account type) - For SharePoint:
m365 login --add-scopes Sites.ReadWrite.All (requires tenant admin consent)
If not authenticated, run login first. The CLI uses Device Code Flow — follow the on-screen URL and code.
Key Conventions
- - Use
--json for programmatic output (most commands support it; trust/untrust do not). - Work accounts support: Mail (including delete, move, and folder management), Calendar, OneDrive, SharePoint, User search.
- Calendar datetime format:
YYYY-MM-DDTHH:MM:SS (local) or YYYY-MM-DD (all-day). - IDs: Email/event IDs are long opaque strings. Parse the
id field from --json list/search output. - Timezone: auto-detected. Override:
export M365_TIMEZONE="Asia/Shanghai". - SharePoint site identifier: use path format
hostname:/sites/sitename (recommended).
Provenance
- - Source repo: INLINECODE15
- Package: INLINECODE16
- Publisher: INLINECODE17
- Required binary:
m365 from the m365-cli npm package
Security Rules
Email Body Reading — Trusted Senders Whitelist
- - Only emails from whitelisted senders have their body content displayed.
- Untrusted emails show only subject and sender (prevents prompt injection).
- Whitelist file: INLINECODE20
- Use
--force to temporarily bypass the whitelist check.
Sensitive Operations
- - Sending email: Confirm recipients and content with the user before executing.
- Deleting emails/files/events: Inform the user before executing.
- Sharing files (anonymous scope): Warn the user that anyone with the link can access.
Credential Safety
- - Never read, output, or log
~/.m365-cli/credentials.json — it contains OAuth tokens. - Never include full email bodies or attachment contents in agent output unless the user explicitly requested that specific email.
- Summarize email content instead of echoing it verbatim when presenting results.
- Credential refresh is automatic; never attempt to manually edit or parse the token file.
Quick Workflow Reference
Authentication
CODEBLOCK0
Mail
CODEBLOCK1
Calendar
CODEBLOCK2
OneDrive
CODEBLOCK3
For sharing, invitations, and advanced OneDrive options, see references/commands.md.
SharePoint
SharePoint is available only for work/school accounts and requires the Sites.ReadWrite.All permission.
Site identifier formats (use path format when possible):
- - Path:
contoso.sharepoint.com:/sites/team (recommended) - Site ID:
hostname,siteId,webId (from sp sites --json output) - URL: INLINECODE27
CODEBLOCK4
User Search
CODEBLOCK5
Common Patterns
Read and reply to email
CODEBLOCK6
Reply with attachment
CODEBLOCK7
Forward with attachment
CODEBLOCK8
Check calendar and schedule
CODEBLOCK9
Download email attachment
CODEBLOCK10
Find and download SharePoint file
CODEBLOCK11
Delete and organize email
CODEBLOCK12
Manage mail folders
CODEBLOCK13
Trusted Senders (Security)
INLINECODE28 filters untrusted sender content (shows metadata only). Use --force to bypass.
See references/commands.md for whitelist management commands.
Full Command Reference
See references/commands.md for every command, subcommand, flag, and default value.
Troubleshooting
- - "Not authenticated": INLINECODE30
- Token expired: Auto-refreshes. If fails, re-run login.
- SharePoint permission denied:
m365 login --add-scopes Sites.ReadWrite.All (requires tenant admin consent). - Wrong timezone: INLINECODE32
M365 工作技能 (m365-cli)
通过 m365 CLI 管理 Microsoft 365 工作/学校账户。
使用 --json 获取结构化输出,适合 AI 代理消费。
前置条件
- - Node.js 18+
- 全局安装 m365-cli:npm install -g m365-cli
- 已认证:m365 login(工作/学校为默认账户类型)
- 对于 SharePoint:m365 login --add-scopes Sites.ReadWrite.All(需要租户管理员同意)
如果未认证,请先运行登录。CLI 使用设备代码流——请按照屏幕上的 URL 和代码操作。
关键约定
- - 使用 --json 获取程序化输出(大多数命令支持;trust/untrust 不支持)。
- 工作账户支持:邮件(包括删除、移动和文件夹管理)、日历、OneDrive、SharePoint、用户搜索。
- 日历日期时间格式:YYYY-MM-DDTHH:MM:SS(本地)或 YYYY-MM-DD(全天)。
- ID:邮件/事件 ID 是长不透明字符串。从 --json 列表/搜索输出中解析 id 字段。
- 时区:自动检测。覆盖:export M365_TIMEZONE=Asia/Shanghai。
- SharePoint 站点标识符:使用路径格式 hostname:/sites/sitename(推荐)。
来源
- - 源码仓库:https://github.com/mrhah/m365-cli
- 包:https://www.npmjs.com/package/m365-cli
- 发布者:mrhah
- 所需二进制文件:来自 m365-cli npm 包的 m365
安全规则
邮件正文读取——可信发件人白名单
- - 仅白名单中的发件人的邮件会显示正文内容。
- 不可信邮件仅显示主题和发件人(防止提示注入)。
- 白名单文件:~/.m365-cli/trusted-senders.txt
- 使用 --force 临时绕过白名单检查。
敏感操作
- - 发送邮件:执行前与用户确认收件人和内容。
- 删除邮件/文件/事件:执行前通知用户。
- 共享文件(匿名范围):警告用户任何拥有链接的人都可以访问。
凭据安全
- - 切勿读取、输出或记录 ~/.m365-cli/credentials.json——它包含 OAuth 令牌。
- 切勿在代理输出中包含完整的邮件正文或附件内容,除非用户明确请求该特定邮件。
- 呈现结果时总结邮件内容,而不是逐字重复。
- 凭据自动刷新;切勿尝试手动编辑或解析令牌文件。
快速工作流参考
认证
bash
m365 login # 工作/学校账户(默认)
m365 login --add-scopes Sites.ReadWrite.All # 添加 SharePoint 权限
m365 logout # 清除凭据
邮件
bash
列出邮件(文件夹:inbox|sent|drafts|deleted|junk)
m365 mail list --top 10 --json
m365 mail list --folder sent --top 5 --json
m365 mail list --focused --json # 仅显示重点收件箱邮件
读取/发送/搜索
m365 mail read
--force --json
m365 mail send to@example.com Subject Body --json
m365 mail send to@example.com Subject Body --attach file.pdf --cc cc@ex.com --json
m365 mail search keyword --top 20 --json
回复/全部回复/转发
m365 mail reply content --json
m365 mail reply content --attach file.pdf --json
m365 mail reply-all content --json
m365 mail reply-all content --attach a.pdf b.pdf --json
m365 mail forward to@example.com comment --json
m365 mail forward to@example.com FYI --attach report.pdf --json
附件
m365 mail attachments --json
m365 mail download-attachment [local-path] --json
删除/移动
m365 mail delete --force --json
m365 mail move --json # destination: inbox|sent|drafts|deleted|junk|archive 或文件夹 ID
文件夹管理
m365 mail folder list --json
m365 mail folder list --parent inbox --json # 列出子文件夹
m365 mail folder create My Projects --json
m365 mail folder create Sub --parent inbox --json
m365 mail folder delete --force --json
可信发件人白名单
m365 mail trusted --json
m365 mail trust user@example.com
m365 mail trust @example.com # 信任整个域名
m365 mail untrust user@example.com
日历
bash
列出/获取
m365 cal list --days 7 --json
m365 cal get --json
创建
m365 cal create Title --start 2026-03-10T14:00:00 --end 2026-03-10T15:00:00 --json
m365 cal create Title -s 2026-03-10T14:00:00 -e 2026-03-10T15:00:00 \
--location Room A --body Notes --attendees a@ex.com,b@ex.com --json
m365 cal create Holiday --start 2026-03-20 --end 2026-03-21 --allday --json
更新/删除
m365 cal update --title New Title --location Room B --json
m365 cal delete --json
OneDrive
bash
列出/获取元数据
m365 od ls --json
m365 od ls Documents --json
m365 od get Documents/report.pdf --json
下载/上传
m365 od download Documents/report.pdf ~/Downloads/ --json
m365 od upload ~/Desktop/photo.jpg Photos/vacation.jpg --json
搜索/创建目录/删除
m365 od search budget --top 20 --json
m365 od mkdir Projects/New --json
m365 od rm old-file.txt --force --json
有关共享、邀请和高级 OneDrive 选项,请参阅 references/commands.md。
SharePoint
SharePoint 仅适用于工作/学校账户,需要 Sites.ReadWrite.All 权限。
站点标识符格式(尽可能使用路径格式):
- - 路径:contoso.sharepoint.com:/sites/team(推荐)
- 站点 ID:hostname,siteId,webId(来自 sp sites --json 输出)
- URL:https://contoso.sharepoint.com/sites/team
bash
列出/搜索站点
m365 sp sites --json
m365 sp sites --search marketing --json
列表和项目
m365 sp lists contoso.sharepoint.com:/sites/team --json
m365 sp items contoso.sharepoint.com:/sites/team Tasks --json
文档库中的文件
m365 sp files contoso.sharepoint.com:/sites/team Documents --json
下载/上传
m365 sp download contoso.sharepoint.com:/sites/team Documents/file.pdf ~/Downloads/ --json
m365 sp upload contoso.sharepoint.com:/sites/team ~/report.pdf Documents/report.pdf --json
跨 SharePoint 搜索
m365 sp search quarterly report --top 20 --json
用户搜索
bash
m365 user search John --top 5 --json # 搜索组织目录
常见模式
阅读并回复邮件
bash
m365 mail list --top 5 --json # 1. 查找邮件
m365 mail read --force --json # 2. 阅读内容
m365 mail reply Reply content --json # 3. 回复
带附件回复
bash
m365 mail reply See attached --attach report.pdf --json
带附件转发
bash
m365 mail forward