PowerShell for Windows Admins

Sep 4 2014   1:07PM GMT

OneNote and XML–finding notebooks

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


When OneNote first came out there wasn’t an API for it as you get for Word or Excel. A community module enabled you to work with the XML that formed the OneNote note books but it wasn’t updated after Office 2007 and doesn’t really work with later versions of OneNote.

I was looking at the Office developer site and noticed that there was some information on OneNote http://msdn.microsoft.com/en-us/library/office/jj680118(v=office.15).aspx.  This series will investigate how to script against OneNote and also explain how to use Select-XML and XPath on the way.

A good starting point would be to discover the OneNote notebooks:

$onenote = New-Object -ComObject OneNote.Application
$scope = [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsPages
[ref]$xml = ”

$onenote.GetHierarchy($null, $scope, $xml)

$schema = @{one=”http://schemas.microsoft.com/office/onenote/2013/onenote”}
$xpath = “//one:Notebook”
Select-Xml -Xml (

$xml.Value) -Namespace $schema -XPath $xpath |
foreach {

The starting point – like all good Office applications – is a COM object that exposes the OneNote object model.  As an aside isn’t time office moved away from COM and we had a proper .NET API or even better a PowerShell module.

You also need to define the scope – in this case get all pages. The enumeration is described here http://msdn.microsoft.com/en-us/library/office/jj680119(v=office.15).aspx

You also need to create a [ref] object to hold the output.

The GetHierarchy() method is used to read through the notebooks. The $null argument means start at the top.

The schema can be found inside the XML produced so to avoid a circular argument I’ll set that in a variable – it has to be a hash table as shown.

Define the XPath – in this case get the nodes labelled one:Notebook.

Select-XML will extract the required nodes – notice how the object has to be presented.

A simple foreacch iterates over the nodes which look like this

name             : Personal (Web)
nickname         : Personal (Web)
ID               : {F8CC78D5-9CC3-40C8-847B-96B15E3D6AD2}{1}{B0}
path             :
https://<a path>  (Web)/
lastModifiedTime : 2014-09-04T17:48:07.000Z
color            : #FFD869
Section          : {Quick Notes, Unfiled Notes, PowerShell Summit}
SectionGroup     : SectionGroup

And you can select the name of the notebook.

2  Comments on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • baltmann
    Exception calling "GetHierarchy" with "3" argument(s): "Unable to cast COM object of type
    'Microsoft.Office.Interop.OneNote.Application2Class' to interface type
    'Microsoft.Office.Interop.OneNote.IApplicationCOM'. This operation failed because the QueryInterface call on
    the COM component for the interface with IID '{452AC71A-B655-4967-A208-A4CC39DD7949}' failed due to the
    following error: Interface not registered (Exception from HRESULT: 0x80040155)."
    At line:4 char:1
    + $onenote.GetHierarchy($null, $scope, $xml)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : InvalidCastException
    Select-Xml : Cannot bind parameter 'Xml'. Cannot convert the "" value of type "System.String" to type
    At line:7 char:17
    + Select-Xml -Xml ($xml.Value) -Namespace $schema -XPath $xpath |
    +                 ~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Select-Xml], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SelectXmlCommand
    0 pointsBadges:
  • Richard Siddaway
    I think you have an old post. The interface to OneNote has changed and I couldn't find any documentation last time I looked
    9,750 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: