Ready to get started?

Learn more about the CData ADO.NET Provider for OneNote or download a free trial:

Download Now

Automate OneNote Integration Tasks from PowerShell

Are you looking for a quick and easy way to access OneNote data from PowerShell? We show how to use the Cmdlets for OneNote and the CData ADO.NET Provider for OneNote to connect to OneNote data and synchronize, automate, download, and more.

The CData Cmdlets for OneNote are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to OneNote.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the OneNote API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete OneNote data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for OneNote. To access OneNote data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for OneNote.

After obtaining the needed connection properties, accessing OneNote data in PowerShell consists of three basic steps.

OneNote uses the OAuth authentication standard. To authenticate using OAuth, you will need to create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the Help documentation for more information.

PowerShell

  1. Install the module:

    Install-Module OneNoteCmdlets
  2. Connect:

    $onenote = Connect-OneNote -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL"
  3. Search for and retrieve data:

    $id = "Jq74mCczmFXk1tC10GB" $notebooks = Select-OneNote -Connection $onenote -Table "Notebooks" -Where "Id = `'$Id`'" $notebooks

    You can also use the Invoke-OneNote cmdlet to execute SQL commands:

    $notebooks = Invoke-OneNote -Connection $onenote -Query 'SELECT * FROM Notebooks WHERE Id = @Id' -Params @{'@Id'='Jq74mCczmFXk1tC10GB'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for OneNote\lib\System.Data.CData.OneNote.dll")
  2. Connect to OneNote:

    $conn= New-Object System.Data.CData.OneNote.OneNoteConnection("OAuthClientId=MyApplicationId; OAuthClientSecret=MySecretKey; CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the OneNoteDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Id, notebook_displayName from Notebooks" $da= New-Object System.Data.CData.OneNote.OneNoteDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.notebook_displayname }

Update OneNote Data

PowerShell

Update-OneNote -Connection $OneNote -Columns @('Id','notebook_displayName') -Values @('MyId', 'Mynotebook_displayName') -Table Notebooks -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.OneNote.OneNoteCommand("UPDATE Notebooks SET Id='Jq74mCczmFXk1tC10GB' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.OneNote.OneNoteParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert OneNote Data

PowerShell

Add-OneNote -Connection $OneNote -Table Notebooks -Columns @("Id", "notebook_displayName") -Values @("MyId", "Mynotebook_displayName")

ADO.NET

$cmd = New-Object System.Data.CData.OneNote.OneNoteCommand("INSERT INTO Notebooks (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add(new System.Data.CData.OneNote.OneNoteParameter("@myId","Jq74mCczmFXk1tC10GB")) $cmd.ExecuteNonQuery()

Delete OneNote Data

PowerShell

Remove-OneNote -Connection $OneNote -Table "Notebooks" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.OneNote.OneNoteCommand("DELETE FROM Notebooks WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.OneNote.OneNoteParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()