Pipe FTP Data to CSV in PowerShell

Ready to get started?

Download a free trial of the FTP Cmdlets to get started:

 Download Now

Learn more:

FTP Icon FTP Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to FTP. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

Use standard PowerShell cmdlets to access FTP tables.

The CData Cmdlets Module for FTP is a standard PowerShell module offering straightforward integration with FTP. Below, you will find examples of using our FTP Cmdlets with native PowerShell cmdlets.

Creating a Connection to Your FTP Data

To connect to FTP or SFTP servers, specify at least RemoteHost and FileProtocol. Specify the port with RemotePort.

Set User and Password to perform Basic authentication. Set SSHAuthMode to use SSH authentication. See the Getting Started section of the data provider help documentation for more information on authenticating via SSH.

Set SSLMode and SSLServerCert to secure connections with SSL.

The data provider lists the tables based on the available folders in your FTP server. Set the following connection properties to control the relational view of the file system:

  • RemotePath: Set this to the current working directory.
  • TableDepth: Set this to control the depth of folders to list as views.
  • FileRetrievalDepth: Set this to retrieve and list files recursively from the root table.

Stored Procedures are available to download files, upload files, and send protocol commands. See the Data Model chapter of the FTP data provider documentation for more information.

$conn = Connect-FTP  -RemoteHost "$RemoteHost"

Selecting Data

Follow the steps below to retrieve data from the MyDirectory table and pipe the result into to a CSV file:

Select-FTP -Connection $conn -Table MyDirectory | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myMyDirectoryData.csv -NoTypeInformation

You will notice that we piped the results from Select-FTP into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-FTP -Connection $conn -Table MyDirectory -Where "FilePath = /documents/doc.txt" | Remove-FTP

Inserting and Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into FTP, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyMyDirectoryUpdates.csv | %{
  $record = Select-FTP -Connection $FTP -Table MyDirectory -Where ("Id = `'"+$_.Id+"`'")
    Update-FTP -Connection $ftp -Table MyDirectory -Columns ("Filesize","Filename") -Values ($_.Filesize, $_.Filename) -Where ("Id = `'"+$_.Id+"`'")
    Add-FTP -Connection $ftp -Table MyDirectory -Columns ("Filesize","Filename") -Values ($_.Filesize, $_.Filename)

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!