Automate Office 365 Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

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!

Are you looking for a quick and easy way to access Office 365 data from PowerShell? We show how to use the Cmdlets for Office 365 and the CData ADO.NET Provider for Office 365 to connect to Office 365 data and synchronize, automate, download, 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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Office 365 API, 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.

After obtaining the needed connection properties, accessing Office 365 data in PowerShell consists of three basic 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.


  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'}


  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 $ $_.size }

Update Office 365 Data


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


$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 System.Data.CData.Office365.Office365Parameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Office 365 Data


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


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

Delete Office 365 Data


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


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