Ready to get started?

Download a free trial of the Office 365 Data Provider to get started:

 Download Now

Learn more:

Office 365 Icon Office 365 ADO.NET Provider

The Office 365 Data Provider gives developers the power to easily connect .NET applications to Microsoft Office 365 data including Outlook Mail, Contact, Calendar, Files, and more!

Automate Office 365 Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Office 365 data from PowerShell? This article demonstrates how to utilize the Office 365 Cmdlets for tasks like connecting to Office 365 data, automating operations, downloading data, and more.

The CData Cmdlets for Office 365 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 Office 365.

PowerShell Cmdlets or ADO.NET Provider?

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

Once you have acquired the necessary connection properties, accessing Office 365 data in PowerShell can be enabled in three steps.

Office 365 uses the OAuth authentication standard. To authenticate requests, you will need to obtain the OAuthClientId, OAuthClientSecret, and OAuthCallbackURL by registering an app with Office 365. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.

PowerShell

  1. Install the module:

    Install-Module Office365Cmdlets
  2. Connect:

    $office365 = Connect-Office365 -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -OAuthCallbackURL "$OAuthCallbackURL"
  3. Search for and retrieve data:

    $userid = "54f34750-0d34-47c9-9949-9fac4791cddb" $files = Select-Office365 -Connection $office365 -Table "Files" -Where "UserId = `'$UserId`'" $files

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

    $files = Invoke-Office365 -Connection $office365 -Query 'SELECT * FROM Files WHERE UserId = @UserId' -Params @{'@UserId'='54f34750-0d34-47c9-9949-9fac4791cddb'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.Office365.Office365Connection("OAuthClientId=MyApplicationId;OAuthClientSecret=MyAppKey;OAuthCallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the Office365DataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, Size from Files" $da= New-Object System.Data.CData.Office365.Office365DataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.size }

Update Office 365 Data

PowerShell

Update-Office365 -Connection $Office365 -Columns @('Name','Size') -Values @('MyName', 'MySize') -Table Files -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Office365.Office365Command("UPDATE Files SET UserId='54f34750-0d34-47c9-9949-9fac4791cddb' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Office 365 Data

PowerShell

Add-Office365 -Connection $Office365 -Table Files -Columns @("Name", "Size") -Values @("MyName", "MySize")

ADO.NET

$cmd = New-Object System.Data.CData.Office365.Office365Command("INSERT INTO Files (UserId) VALUES (@myUserId)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myUserId","54f34750-0d34-47c9-9949-9fac4791cddb"))) $cmd.ExecuteNonQuery()

Delete Office 365 Data

PowerShell

Remove-Office365 -Connection $Office365 -Table "Files" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Office365.Office365Command("DELETE FROM Files WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()