# 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) Function ReplaceIllegal { param ( $text ) $illegal = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\' $replaced = $text -replace "[$illegal]",'_' return $replaced } Write-Output $Hierarchy.Notebooks.Notebook.Roser.name function LoopAndExport { # 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.ID Write-Host $notebook.InnerXml $destinationFile = Join-Path -Path $folder -ChildPath $name".test.xml" write-output $destinationFile $notebookInnerXml = $notebook.InnerXml $exportedXML = "$notebookInnerXml" $exportedXML |Out-File $destinationFile }else{} } } LoopAndExport