CSV files store data in a simple table format. Import-Csv and Export-Csv help you read and write these files easily in PowerShell.
CSV operations (Import-Csv, Export-Csv) in PowerShell
Import-Csv -Path <filepath> Export-Csv -Path <filepath> [-NoTypeInformation]
Import-Csv reads a CSV file and creates objects with properties from the columns.
Export-Csv converts objects into CSV format and saves to a file. Use -NoTypeInformation to skip extra type info line.
users.csv and creates objects for each row.Import-Csv -Path "users.csv"$data to output.csv without extra type info.$data | Export-Csv -Path "output.csv" -NoTypeInformationdata.csv and filters rows where Age is greater than 30.Import-Csv -Path "data.csv" | Where-Object { [int]$_.Age -gt 30 }
This script reads a CSV file with user data, filters users older than 25, saves the filtered list to a new CSV file, and prints their details.
## Sample CSV content saved as users.csv: # Name,Age,City # Alice,28,New York # Bob,35,Chicago # Carol,22,Seattle # Import CSV file $users = Import-Csv -Path "users.csv" # Filter users older than 25 $olderUsers = $users | Where-Object { [int]$_.Age -gt 25 } # Export filtered users to new CSV $olderUsers | Export-Csv -Path "older_users.csv" -NoTypeInformation # Display filtered users $olderUsers | ForEach-Object { "Name: $($_.Name), Age: $($_.Age), City: $($_.City)" }
CSV files must have a header row with column names for Import-Csv to work properly.
Export-Csv adds a type information line by default; use -NoTypeInformation to avoid this.
Imported CSV data is treated as strings; convert types if needed (e.g., [int]$_.Age).
Use Import-Csv to read CSV files into objects you can work with in PowerShell.
Use Export-Csv to save objects back into CSV files for sharing or reports.
Filtering and processing CSV data is easy by combining these commands with PowerShell pipelines.