Posted on:
Categories: PowerShell;SharePoint
After discovering that migrating OneNote notebooks from one SharePoint location to another is difficult to do while maintaining the integrity of the notebooks (, I needed to retrieve an inventory of all OneNote notebooks that reside on a collection of Office365 personal sites. 

Once I had a list of OneNote notebooks I could begin the process of migrating them in such a way that their integrity is maintained.

The following script uses the Client Side Object Model to find all OneNote notebooks. It will export a list of OneNote notebooks to a csv file.

​Make sure that the script is run in the same directory as the SharePoint Client dlls.

#Search for all OneNote files in a collection of Office365 sites

$global:notebooks = @()

Add-Type -Path "Microsoft.SharePoint.Client.dll" 
Add-Type -Path "Microsoft.SharePoint.Client.Runtime.dll" 

$cred = Get-Credential -Message "Enter your credentials for SharePoint Online:" 
$global:spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
Function Get-OneNoteInventory($url){
 $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
 $clientContext.Credentials = $spoCred
 $list = $clientContext.Web.Lists.GetByTitle("Documents")
 $query = New-Object Microsoft.SharePoint.Client.CamlQuery
 $query.ViewXml = "
     <FieldRef Name='HTML_x0020_File_x0020_Type' />
     <Value Type='Computed'>OneNote.Notebook</Value>
 $listItems = $list.GetItems($query)
 $listUrl = $list.Context.Url
 foreach ($listItem in $listItems)
  $fullUrl = $listUrl + $listItem["FileRef"]
  $o = new-object psobject
  $o | Add-Member -MemberType noteproperty -Name Name -value $listItem['Title']
  $o | Add-Member -MemberType noteproperty -Name Path -value $fullUrl
  $global:notebooks += $o

$urls = @(

foreach($url in $urls){
 Get-OneNoteInventory -Url $url

$global:notebooks | export-csv "OneNote_Inventory.csv" -noTypeInformation