commit 4d5f399a7c759feb4515851a794dc677e0906a9f Author: pi-ux-ce Date: Tue Jan 20 16:36:04 2026 +0000 Add OneNoteXmlExport.ps1 diff --git a/OneNoteXmlExport.ps1 b/OneNoteXmlExport.ps1 new file mode 100644 index 0000000..b9dd270 --- /dev/null +++ b/OneNoteXmlExport.ps1 @@ -0,0 +1,139 @@ + + + + + +# https://stackoverflow.com/questions/53689087/powershell-and-onenote +# http://thebackend.info/powershell/2017/12/onenote-read-and-write-content-with-powershell/ +# https://stackoverflow.com/questions/53639041/how-to-access-contents-of-onenote-page + +# Get export folder +Function Get-Folder($initialDirectory) { + [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null + $foldername = New-Object System.Windows.Forms.FolderBrowserDialog + $foldername.Description = "Select an export folder" + $foldername.rootfolder = "MyComputer" + if($foldername.ShowDialog() -eq "OK") + { + $folder += $foldername.SelectedPath + } + return $folder +} + +# Spider and find each page, create directory for each group +Function Spider-OneNote-Notebook { + param( $onenote, $node, $path ) + foreach($child in $node.ChildNodes) { + if ($child.HasChildNodes) { + if ($child.isRecycleBin -ne 'true') { + $folder = Join-Path -Path $path -ChildPath $child.name + New-Item -Path $folder -ItemType directory | Out-Null + Write-Host "Section: $($folder)" + Spider-OneNote-Notebook -onenote $onenote -node $child -path $folder + } + } else { + Export-OneNote-Page -onenote $onenote -node $child -path $path + } + } +} + +# Export page +Function Export-OneNote-Page { + param( $onenote, $node, $path ) + # Replace invalid file characters + $name = ReplaceIllegal -text $node.name + $file = $(Join-Path -Path $path -ChildPath "$($name).htm") + Write-Host "Page: $($file)" + # Export + $onenote.Publish($node.ID, $file, 7, "") + Export-OneNote-Attachments -onenote $onenote -node $node -path $path +} + +# Copy embedded attachments +Function Export-OneNote-Attachments { + param ( $onenote, $node, $path ) + $xml = '' + $schema = @{one=”http://schemas.microsoft.com/office/onenote/2013/onenote”} + $onenote.GetPageContent($node.ID, [ref]$xml) + $xml | Select-Xml -XPath "//one:Page/one:Outline/one:OEChildren/one:OE/one:InsertedFile" -Namespace $schema | foreach { + $file = Join-Path -Path $path -ChildPath $_.Node.preferredName + Write-Host "Attachment: $($file)" + Copy-Item $_.Node.pathCache -Destination $file + } +} + +Function ReplaceIllegal { + param ( $text ) + $illegal = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\' + $replaced = $text -replace "[$illegal]",'_' + return $replaced +} + +# Get export folder +$folder = "C:\Users\pgeeraert\Downloads\export-onenote" + +# Connect +$OneNote = New-Object -ComObject OneNote.Application +[xml]$Hierarchy = "" +$OneNote.GetHierarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy) + +Write-Output $Hierarchy.Notebooks.Notebook.Roser.name + +# Loop over each notebook +foreach ($notebook in $Hierarchy.Notebooks.Notebook ) { + $name = ReplaceIllegal -text $notebook.name + + if($name -ccontains "Deployments"){ + Write-Output $name + <# + Write-Host $notebook.ChildNodes + Write-Host $notebook.Attributes + Write-Host $notebook.BaseURI + Write-Host $notebook.color + Write-Host $notebook.FirstChild + Write-Host $notebook.HasAttributes + Write-Host $notebook.HasChildNodes + Write-Host $notebook.ID + Write-Host $notebook.InnerText + Write-Host $notebook.InnerXml + Write-Host $notebook.IsEmpty + Write-Host $notebook.IsReadOnly + Write-Host $notebook.LastChild + Write-Host $notebook.lastModifiedTime + Write-Host $notebook.LocalName + Write-Host $notebook.name + Write-Host $notebook.NamespaceURI + Write-Host $notebook.NextSibling + Write-Host $notebook.nickname + Write-Host $notebook.NodeType + Write-Host $notebook.OwnerDocument + Write-Host $notebook.OuterXml + Write-Host $notebook.ParentNode + Write-Host $notebook.path + Write-Host $notebook + + $nf = Join-Path -Path $folder -ChildPath $name + Write-Host "Notebook: $($nf)" + #> + + Write-Host $notebook.InnerXml + + + $destinationFile = Join-Path -Path $folder -ChildPath $name".xml" + + write-output $destinationFile + + $notebook.InnerXml |Out-File $destinationFile + }else{} + +} + + +# Loop over each notebook +#foreach ($notebook in $Hierarchy.Notebooks.Notebook ) { +# $name = ReplaceIllegal -text $notebook.name +# $nf = Join-Path -Path $folder -ChildPath $name +# Write-Host "Notebook: $($nf)" +# New-Item -Path $nf -ItemType directory | Out-Null +# Spider-OneNote-Notebook -onenote $OneNote -node $notebook -path $nf +#}