Pipe USPS Data to CSV in PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

USPS Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to USPS. 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 USPS tables.

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

Creating a Connection to Your USPS Data

To authenticate with USPS, set the following connection properties.

  • PostageProvider: The postage provider to use to process requests. Available options are ENDICIA and STAMPS. If unspecified, this property will default to ENDICIA.
  • UseSandbox: This controls whether live or test requests are sent to the production or sandbox servers. If set to true, the Password, AccountNumber, and StampsUserId properties are ignored.
  • StampsUserId: This value is used for logging into authentication to the Stamps servers. This value is not applicable for Endicia and is optional if UseSandbox is true.
  • Password: This value is used for logging into Endicia and Stamps servers. If the postage provider is Endicia, this will be the pass phrase associated with your postage account. It is optional if UseSandbox is true.
  • AccountNumber: The shipper's account number. It is optional if UseSandbox is true.
  • PrintLabelLocation: This property is required to use the GenerateLabels or GenerateReturnLabels stored procedures. This should be set to the folder location where generated labels should be stored.

The Cache Database

Many of the useful task available from USPS require a lot of data. To ensure this data is easy to input and recall later, utilize a cache database to make requests. Set the cache connection properties in order to use the cache:

  • CacheLocation: The path to the cache location, for which a connection will be configured with the default cache provider. For example, C:\users\username\documents\uspscache

As an alternative to CacheLocation, set the combination of CacheConnection and CacheProvider to configure a cache connection using a provider separate from the default.

$conn = Connect-USPS  -PostageProvider "$PostageProvider" -RequestId "$RequestId" -Password "$Password" -AccountNumber "$AccountNumber"

Selecting Data

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

Select-USPS -Connection $conn -Table Senders | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySendersData.csv -NoTypeInformation

You will notice that we piped the results from Select-USPS 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-USPS -Connection $conn -Table Senders -Where "SenderID = 25" | Remove-USPS

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 USPS, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MySendersUpdates.csv | %{
  $record = Select-USPS -Connection $USPS -Table Senders -Where ("Id = `'"+$_.Id+"`'")
    Update-USPS -Connection $usps -Table Senders -Columns ("FirstName","Phone") -Values ($_.FirstName, $_.Phone) -Where ("Id = `'"+$_.Id+"`'")
    Add-USPS -Connection $usps -Table Senders -Columns ("FirstName","Phone") -Values ($_.FirstName, $_.Phone)

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!