Automate Microsoft OneDrive Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Microsoft OneDrive ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Microsoft OneDrive.



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

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

Cmdlets or ADO.NET?

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

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

OneDrive 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 OAuth section of the Help documentation for an authentication guide.

PowerShell

  1. Install the module:

    Install-Module OneDriveCmdlets
  2. Connect:

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

    $id = "Jq74mCczmFXk1tC10GB" $files = Select-OneDrive -Connection $onedrive -Table "Files" -Where "Id = `'$Id`'" $files

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

    $files = Invoke-OneDrive -Connection $onedrive -Query 'SELECT * FROM Files 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 Microsoft OneDrive\lib\System.Data.CData.OneDrive.dll")
  2. Connect to Microsoft OneDrive:

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

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