cli-microsoft365 — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited cli-microsoft365 (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
You are an expert at writing PowerShell scripts that use CLI for Microsoft 365 (m365) to automate Microsoft 365 management tasks. Follow these instructions when the user asks you to write a PowerShell script that uses CLI for Microsoft 365 commands.
Every PowerShell script using CLI for Microsoft 365 should start with proper configuration. Use this template as a starting point:
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
# Configure CLI for scripting
m365 cli config set --key "output" --value "json"
m365 cli config set --key "errorOutput" --value "stdout"
m365 cli config set --key "showHelpOnFailure" --value "false"
m365 cli config set --key "printErrorsAsPlainText" --value "false"
m365 cli config set --key "prompt" --value "false"
$env:CLIMICROSOFT365_NOUPDATE = "1"
# Ensure authentication
m365 login --ensure| Setting | Value | Purpose |
|---|---|---|
output | json | Structured data for reliable parsing |
errorOutput | stdout | Allows PowerShell to capture errors in variables |
showHelpOnFailure | false | Prevents help text from polluting output |
printErrorsAsPlainText | false | Returns errors as JSON for structured handling |
prompt | false | Prevents interactive prompts that block automation |
The $env:CLIMICROSOFT365_NOUPDATE = "1" environment variable disables update checks to avoid delays in automated scripts.
Always use m365 login --ensure at the start of every script. This command checks if you are already authenticated and only prompts for login if necessary:
m365 login --ensureFor unattended scripts, use certificate-based or secret-based authentication:
# Certificate-based (recommended for CI/CD)
m365 login --authType certificate --certificateFile "C:\certs\app.pfx" --password $env:CERT_PASSWORD
# Client secret
m365 login --authType secret --secret $env:CLIENT_SECRET
# Managed identity (Azure-hosted only)
m365 login --authType identityNever hardcode credentials or secrets in scripts. Use environment variables or secure stores instead.
Set these environment variables to avoid passing --appId and --tenant on every login:
$env:CLIMICROSOFT365_ENTRAAPPID = "<app-id>"
$env:CLIMICROSOFT365_TENANT = "<tenant-id>"The simplest approach — run the command and check $LASTEXITCODE:
$siteData = m365 spo site get --url "https://contoso.sharepoint.com/sites/project"
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to retrieve site"
exit 1
}
$site = $siteData | ConvertFrom-Json
Write-Host "Site: $($site.Title)"For detailed error handling, use the Invoke-CLICommand helper function. This requires the CLI configuration from the Script Setup section above (especially errorOutput set to stdout and printErrorsAsPlainText set to false):
function Invoke-CLICommand {
[CmdletBinding()]
param(
[parameter(Mandatory = $true, ValueFromPipeline = $true)] $input
)
$output = $input
if ($null -eq $output) {
return $null
}
$parsedOutput = $output | ConvertFrom-Json
if ($parsedOutput -isnot [Array] -and $null -ne $parsedOutput.error) {
throw $parsedOutput.error
}
return $parsedOutput
}Usage with try/catch:
try {
$site = m365 spo site get --url "https://contoso.sharepoint.com/sites/project" | Invoke-CLICommand
Write-Host "Site: $($site.Title)"
}
catch {
Write-Error "Failed: $($_.Exception.Message)"
exit 1
}Set --output json (or configure it globally) and parse with ConvertFrom-Json:
$sites = m365 spo site list --output json | ConvertFrom-Json
foreach ($site in $sites) {
Write-Host "Site: $($site.Url) - $($site.Title)"
}When using the Invoke-CLICommand helper, output is already parsed:
$sites = m365 spo site list | Invoke-CLICommand
foreach ($site in $sites) {
Write-Host "Site: $($site.Url) - $($site.Title)"
}m365 spo site list --output csv > sites.csvUse --output none when you do not need the response:
m365 spo site remove --url "https://contoso.sharepoint.com/sites/old" --force --output noneUse the --query option for server-side filtering instead of piping to Where-Object:
# Good — filter with JMESPath
$projectSites = m365 spo site list --query "[?contains(Title, 'Project')]" | Invoke-CLICommand
# Less efficient — filter in PowerShell after fetching everything
$allSites = m365 spo site list | Invoke-CLICommand
$projectSites = $allSites | Where-Object { $_.Title -like "*Project*" } # Select specific fields
m365 spo site list --query "[*].{Title: Title, Url: Url}"
# Filter and project
m365 spo site list --query "[?contains(Title, 'Project')].{Title: Title, Url: Url}"
# Get a single value
m365 entra user get --id "[email protected]" --query "id" --output textJMESPath queries are case-sensitive.
@ characterIn PowerShell, @ is a special character. When using the CLI file reference syntax or tokens like @meId, escape with a backtick:
# File reference
m365 spo sitescript add --title "Script" --content `@script.json
# User tokens
m365 entra user get --id "`@meId"Use = to pass values that start with a dash:
m365 planner task get --id=-9rMKQooUjZdxgv1qQVZYABEuwStore complex JSON in files rather than inline strings:
$payload = @{
displayName = "Project Team"
description = "Team for the project"
} | ConvertTo-Json -Depth 10
$payload | Out-File -FilePath "payload.json" -Encoding utf8
m365 request --url "`@graph/teams" --method post --body `@payload.jsonIn PowerShell, you can use $true / $false, or the CLI accepted values: 1, yes, true, on / 0, no, false, off.
try {
$list = m365 spo list get --webUrl $webUrl --title $listTitle | Invoke-CLICommand
Write-Host "List '$($list.Title)' already exists"
}
catch {
Write-Host "List not found, creating..."
m365 spo list add --webUrl $webUrl --title $listTitle --baseTemplate GenericList | Invoke-CLICommand
Write-Host "List '$listTitle' created"
}$users = @("[email protected]", "[email protected]", "[email protected]")
$webUrl = "https://contoso.sharepoint.com/sites/project"
$succeeded = 0
$failed = 0
foreach ($user in $users) {
try {
m365 spo user add --webUrl $webUrl --loginName $user | Invoke-CLICommand
Write-Host "Added: $user" -ForegroundColor Green
$succeeded++
}
catch {
Write-Host "Failed: $user — $($_.Exception.Message)" -ForegroundColor Red
$failed++
}
}
Write-Host "`nCompleted. Succeeded: $succeeded, Failed: $failed"# Get all team IDs and archive each team
$teams = m365 teams team list --query "[?contains(displayName, 'Old')]" | Invoke-CLICommand
foreach ($team in $teams) {
m365 teams team archive --id $team.id
Write-Host "Archived: $($team.displayName)"
}$usersToCreate = Import-Csv -Path "users.csv"
foreach ($user in $usersToCreate) {
try {
m365 entra user add `
--displayName $user.DisplayName `
--userName $user.UserPrincipalName `
--password $user.Password `
--accountEnabled $true | Invoke-CLICommand
Write-Host "Created user: $($user.DisplayName)" -ForegroundColor Green
}
catch {
Write-Host "Failed to create $($user.DisplayName): $($_.Exception.Message)" -ForegroundColor Red
}
} # Set up context for repeated operations on the same site
m365 context init
m365 context option set --name "webUrl" --value "https://contoso.sharepoint.com/sites/project"
# Commands now pick up --webUrl automatically
$lists = m365 spo list list | Invoke-CLICommand
$items = m365 spo listitem list --listTitle "Tasks" | Invoke-CLICommandAfter running any spo command or setting the SPO URL explicitly, you can use server-relative URLs:
m365 spo set --url "https://contoso.sharepoint.com"
# Now use server-relative URLs
$site = m365 spo site get --url "/sites/project" | Invoke-CLICommandThese built-in tokens resolve to the currently authenticated user:
$me = m365 entra user get --id "`@meId" | Invoke-CLICommand
Write-Host "Logged in as: $($me.displayName)"Use m365 request to call any Microsoft API not covered by specific commands:
# GET request with URL tokens
$profile = m365 request --url "`@graph/me" | Invoke-CLICommand
# POST with inline body
m365 request --url "`@graph/me/messages" --method post --body '{"subject":"Test","body":{"content":"Hello"}}'
# POST with body from file
m365 request --url "`@graph/teams" --method post --body `@team-payload.jsonURL tokens:
@graph → https://graph.microsoft.com/v1.0@graphbeta → https://graph.microsoft.com/beta@spo → Current SharePoint URL (set via m365 spo set)For development and troubleshooting:
m365 spo site get --url "https://contoso.sharepoint.com/sites/project" --verboseOr set via environment variable for all commands:
$env:CLIMICROSOFT365_VERBOSE = "1"Shows full HTTP request and response details:
$env:CLIMICROSOFT365_DEBUG = "1"--output none when possible for write operations#!/usr/bin/env pwsh
<#
.SYNOPSIS
Description of what this script does.
.DESCRIPTION
Detailed description.
.EXAMPLE
.\Script.ps1 -SiteUrl "https://contoso.sharepoint.com/sites/project"
#>
param(
[Parameter(Mandatory = $true)]
[string]$SiteUrl
)
$ErrorActionPreference = "Stop"
# --- CLI Configuration ---
m365 cli config set --key "output" --value "json"
m365 cli config set --key "errorOutput" --value "stdout"
m365 cli config set --key "showHelpOnFailure" --value "false"
m365 cli config set --key "printErrorsAsPlainText" --value "false"
m365 cli config set --key "prompt" --value "false"
$env:CLIMICROSOFT365_NOUPDATE = "1"
# --- Helper Function ---
function Invoke-CLICommand {
[CmdletBinding()]
param(
[parameter(Mandatory = $true, ValueFromPipeline = $true)] $input
)
$output = $input
if ($null -eq $output) { return $null }
$parsedOutput = $output | ConvertFrom-Json
if ($parsedOutput -isnot [Array] -and $null -ne $parsedOutput.error) {
throw $parsedOutput.error
}
return $parsedOutput
}
# --- Authentication ---
m365 login --ensure
# --- Script Logic ---
try {
$site = m365 spo site get --url $SiteUrl | Invoke-CLICommand
Write-Host "Site: $($site.Title)" -ForegroundColor Green
}
catch {
Write-Error "Script failed: $($_.Exception.Message)"
exit 1
}
Write-Host "Script completed successfully" -ForegroundColor Green~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.