(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to filter and export a CSV in PowerShell

João Barros 05 de July de 2026 4 min read

Receiving a CSV file with hundreds or thousands of rows when you only need part of the data is a daily task on any data team. With PowerShell you can filter and export a CSV in just a few seconds, in a repeatable way and without opening Excel. This guide shows, step by step, how to read a CSV file, apply filters to the rows, and save only the data you need to a new file.

Prerequisites

  • Windows PowerShell 5.1 or PowerShell 7 (either works; to check, run $PSVersionTable.PSVersion).
  • A CSV file with a header on the first line. We will use an example called vendas.csv.
  • Basic command-line knowledge (opening a terminal and moving between folders).

To follow along, create a vendas.csv file with this content:

Data,Produto,Regiao,Quantidade
2026-01-04,Teclado,Norte,12
2026-01-05,Rato,Sul,4
2026-01-06,Monitor,Norte,25
2026-01-07,Teclado,Centro,8
2026-01-08,Monitor,Norte,3

Step 1: Read the CSV with Import-Csv

The first step to filter and export a CSV in PowerShell is to load it into memory. The Import-Csv cmdlet reads the file and turns each line into an object, using the header as the column names.

$dados = Import-Csv -Path ".\vendas.csv"
$dados | Format-Table

If your file uses a semicolon as the separator (common in CSV files exported in Portugal), specify it with the -Delimiter parameter:

$dados = Import-Csv -Path ".\vendas.csv" -Delimiter ";"

Step 2: Filter the rows with Where-Object

Now we filter only the rows that matter. Where-Object goes through each object and keeps the ones that meet the condition. In this example we want only the sales from the Norte region with a quantity of 10 or more.

$filtrado = $dados | Where-Object {
    $_.Regiao -eq "Norte" -and [int]$_.Quantidade -ge 10
}
$filtrado | Format-Table

Notice the [int] before $_.Quantidade. Because Import-Csv reads every value as text, you need to convert it to a number before comparing. Without that conversion, "3" would be considered greater than "25", because the comparison would be done as text.

You can combine several conditions with -and (all must be true) or -or (only one needs to be). This lets you adapt the filter to any business rule.

Step 3: Choose and sort the columns

Before exporting, you can choose which columns to keep and in what order to present the data. Select-Object selects columns and Sort-Object orders the results, for example by quantity in descending order.

$resultado = $filtrado |
    Sort-Object { [int]$_.Quantidade } -Descending |
    Select-Object Data, Produto, Quantidade

Step 4: Export to a new CSV with Export-Csv

Finally, we save the result to a new file. Export-Csv does the opposite of Import-Csv: it turns the objects back into lines of text. Use -NoTypeInformation to avoid writing an extra line of metadata and -Encoding UTF8 to keep accented characters correct.

$resultado | Export-Csv -Path ".\vendas_norte.csv" -NoTypeInformation -Encoding UTF8

If you need the result with a semicolon (to open it directly in a Portuguese Excel), add -Delimiter ";".

Tip: always save the result to a new file instead of overwriting the original. That way, if the filter is wrong, you do not lose your starting data.

Verify the result

To confirm the file is correct, read it again with PowerShell or look at the first lines:

Import-Csv ".\vendas_norte.csv" | Format-Table
Get-Content ".\vendas_norte.csv" -TotalCount 5

You should see only the two rows from the Norte region with a quantity of 10 or more (Monitor with 25 and Teclado with 12), sorted from the highest to the lowest quantity. If the number of rows matches, the filter worked.

Conclusion

With just three cmdlets — Import-Csv, Where-Object and Export-Csv — you have automated a task that, done by hand in Excel, would take much longer and be hard to repeat. The next step is to save these commands in a .ps1 file so you can reuse them whenever a new file arrives. Which other filter would be useful in your daily work: by date, by product, or by a minimum sales value?