使用PowerShell导出OneNote打开笔记本路径失败求助
问题分析与解决方案
核心问题
你的脚本未进入Foreach-Object循环,本质是Get-ChildItem -Path $registryPath没有返回任何结果,常见原因有两个:
- 注册表路径不匹配:如果你的Office是32位版本,而运行的是64位PowerShell,注册表项会被重定向到
Wow6432Node子路径下,原路径找不到子键。 - 注册表结构理解偏差:
Get-ChildItem用于获取注册表路径下的子键,如果笔记本路径直接存储在OpenNotebooks键本身的属性值中(而非子键),该命令自然无输出。
修正后的脚本
场景1:笔记本路径存储在OpenNotebooks的子键中(OneNote标准存储方式)
$registryPath = "HKCU:\SOFTWARE\Microsoft\Office\16.0\OneNote\OpenNotebooks" # 自动适配32/64位Office的注册表路径 if (-not (Test-Path $registryPath)) { $registryPath = "HKCU:\SOFTWARE\Wow6432Node\Microsoft\Office\16.0\OneNote\OpenNotebooks" if (-not (Test-Path $registryPath)) { Write-Error "未找到目标注册表路径" exit } } $filePath = [Environment]::GetFolderPath("MyDocuments") $outFile = Join-Path -Path $filePath -ChildPath "OneNotePaths.txt" # 清空目标文件避免重复追加 Set-Content -Path $outFile -Value $null # 遍历所有子键,提取Path属性 Get-ChildItem -Path $registryPath | ForEach-Object { $notebookProps = Get-ItemProperty -Path $_.PsPath if ($notebookProps.PSObject.Properties.Name -contains "Path") { Add-Content -Path $outFile -Value $notebookProps.Path } } Write-Host "导出完成,文件路径:$outFile"
场景2:笔记本路径直接存储在OpenNotebooks键的属性中
如果你的注册表结构是OpenNotebooks键下直接有多个路径值(而非子键),使用以下脚本:
$registryPath = "HKCU:\SOFTWARE\Microsoft\Office\16.0\OneNote\OpenNotebooks" if (-not (Test-Path $registryPath)) { $registryPath = "HKCU:\SOFTWARE\Wow6432Node\Microsoft\Office\16.0\OneNote\OpenNotebooks" if (-not (Test-Path $registryPath)) { Write-Error "未找到目标注册表路径" exit } } $filePath = [Environment]::GetFolderPath("MyDocuments") $outFile = Join-Path -Path $filePath -ChildPath "OneNotePaths.txt" Set-Content -Path $outFile -Value $null # 提取所有非系统属性的路径值 $keyProps = Get-ItemProperty -Path $registryPath $keyProps.PSObject.Properties | Where-Object { $_.Name -notlike "PS*" } | ForEach-Object { Add-Content -Path $outFile -Value $_.Value } Write-Host "导出完成,文件路径:$outFile"
验证步骤
- 打开注册表编辑器,定位到
HKCU:\SOFTWARE\Microsoft\Office\16.0\OneNote\OpenNotebooks,确认是子键存储路径还是直接属性存储。 - 在PowerShell中单独运行
Get-ChildItem -Path $registryPath,查看是否有子键输出,判断路径是否正确。
内容的提问来源于stack exchange,提问作者J0nsan
相关产品推荐
相关产品推荐

