Control Microsoft OneNote on Windows via PowerShell.
Use Cases
- The user says "Help me create a new note page in OneNote" or "Search OneNote for notes about XXX"
- The user needs to view or organize OneNote content
Command Reference
List Notebooks
$onenote = New-Object -ComObject OneNote.Application
[xml]$hierarchy = ""
$onenote.GetHierarchy("", [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsNotebooks, [ref]$hierarchy)
$hierarchy.Notebooks.Notebook | ForEach-Object { Write-Output "📓 $($.name)" $.Section | ForEach-Object { Write-Output " 📑 $($_.name)" } }
### 搜索笔记
```powershell
$onenote = New-Object -ComObject OneNote.Application
[xml]$results = ""
$onenote.FindPages("", "搜索关键词", [ref]$results)
$results.Pages.Page | ForEach-Object {
Write-Output "$($_.name) — $($_.dateTime)"
}
Create a New Page
$onenote = New-Object -ComObject OneNote.Application
# 获取目标 Section ID
[xml]$hierarchy = ""
$onenote.GetHierarchy("", [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsSections, [ref]$hierarchy)
$sectionId = ($hierarchy.Notebooks.Notebook.Section | Where-Object { $_.name -eq "目标分区" }).ID
# 创建页面
$pageXml = @"
<?xml version="1.0"?>
<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote">
<one:Title>
<one:OE><one:T><![CDATA[笔记标题]]></one:T></one:OE>
</one:Title>
<one:Outline>
<one:OEChildren>
<one:OE><one:T><![CDATA[笔记内容]]></one:T></one:OE>
</one:OEChildren>
</one:Outline>
</one:Page>
"@
$onenote.CreateNewPage($sectionId, [ref]$null)
Notes
- The desktop version of OneNote (not the UWP version) must be installed
- COM object operations require the OneNote process to be running
- First-time use may require user authorization
Security Rules
- Do not delete notebooks or sections
- Before creating/modifying, show the content for the user to confirm