Ready to get started?

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

Download Now

Automate Excel Services Integration Tasks from PowerShell

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

The CData Cmdlets for Excel Services 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 Excel Services.

Cmdlets or ADO.NET?

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

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

The URL, User, and Password properties, under the Authentication section, must be set to valid credentials for SharePoint Online, SharePoint 2010, or SharePoint 2013. Additionally, the Library property must be set to a valid SharePoint Document Library and the File property must be set to a valid .xlsx file in the indicated Library.

PowerShell

  1. Install the module:

    Install-Module ExcelServicesCmdlets
  2. Connect:

    $excelservices = Connect-ExcelServices -URL "$URL" -User "$User" -Password "$Password" -File "$File"
  3. Search for and retrieve data:

    $industry = "Floppy Disks" $account = Select-ExcelServices -Connection $excelservices -Table "Account" -Where "Industry = `'$Industry`'" $account

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

    $account = Invoke-ExcelServices -Connection $excelservices -Query 'SELECT * FROM Account WHERE Industry = @Industry' -Params @{'@Industry'='Floppy Disks'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.ExcelServices.ExcelServicesConnection("URL=https://myorg.sharepoint.com;User=admin@myorg.onmicrosoft.com;Password=password;File=Book1.xlsx;") $conn.Open()
  3. Instantiate the ExcelServicesDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, AnnualRevenue from Account" $da= New-Object System.Data.CData.ExcelServices.ExcelServicesDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.annualrevenue }

Update Excel Services Data

PowerShell

Update-ExcelServices -Connection $ExcelServices -Columns @('Name','AnnualRevenue') -Values @('MyName', 'MyAnnualRevenue') -Table Account -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.ExcelServices.ExcelServicesCommand("UPDATE Account SET Industry='Floppy Disks' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.ExcelServices.ExcelServicesParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Excel Services Data

PowerShell

Add-ExcelServices -Connection $ExcelServices -Table Account -Columns @("Name", "AnnualRevenue") -Values @("MyName", "MyAnnualRevenue")

ADO.NET

$cmd = New-Object System.Data.CData.ExcelServices.ExcelServicesCommand("INSERT INTO Account (Industry) VALUES (@myIndustry)", $conn) $cmd.Parameters.Add(new System.Data.CData.ExcelServices.ExcelServicesParameter("@myIndustry","Floppy Disks")) $cmd.ExecuteNonQuery()

Delete Excel Services Data

PowerShell

Remove-ExcelServices -Connection $ExcelServices -Table "Account" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.ExcelServices.ExcelServicesCommand("DELETE FROM Account WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.ExcelServices.ExcelServicesParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()