Ready to get started?

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

Download Now

Automate CSV Integration Tasks from PowerShell

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

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

Cmdlets or ADO.NET?

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

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

The DataSource property must be set to a valid local folder name. Also, specify the IncludeFiles property to work with text files having extensions that differ from .csv, .tab, or .txt. Specify multiple file extensions in a comma-separated list. You can also set Extended Properties compatible with the Microsoft Jet OLE DB 4.0 driver. Alternatively, you can provide the format of text files in a Schema.ini file.

PowerShell

  1. Install the module:

    Install-Module CSVCmdlets
  2. Connect:

    $csv = Connect-CSV -DataSource "$DataSource"
  3. Search for and retrieve data:

    $firstname = "Bob" $customer = Select-CSV -Connection $csv -Table "Customer" -Where "FirstName = `'$FirstName`'" $customer

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

    $customer = Invoke-CSV -Connection $csv -Query 'SELECT * FROM Customer WHERE FirstName = @FirstName' -Params @{'@FirstName'='Bob'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.CSV.CSVConnection("DataSource=MyCSVFilesFolder;") $conn.Open()
  3. Instantiate the CSVDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT City, TotalDue from Customer" $da= New-Object System.Data.CData.CSV.CSVDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.city $_.totaldue }