Ready to get started?

Download a free trial of the Microsoft OneDrive Data Provider to get started:

 Download Now

Learn more:

Microsoft OneDrive Icon Microsoft OneDrive ADO.NET Provider

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

Automate Microsoft OneDrive Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Microsoft OneDrive data from PowerShell? This article demonstrates how to utilize the Microsoft OneDrive Cmdlets for tasks like connecting to Microsoft OneDrive data, automating operations, downloading data, 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.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to Microsoft OneDrive, 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.

Once you have acquired the necessary connection properties, accessing Microsoft OneDrive data in PowerShell can be enabled in three 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 }