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.
Practice
Import-Csv do?Solution
Step 1: Understand the purpose of Import-Csv
Import-Csvreads data from a CSV file and creates objects where each row is an object with properties matching the CSV headers.Step 2: Compare with other options
Export-Csvsaves objects to CSV, deletion and JSON conversion are unrelated toImport-Csv.Final Answer:
Reads a CSV file and converts each row into an object -> Option AQuick Check:
Import-Csv = Reads CSV to objects [OK]
- Confusing Import-Csv with Export-Csv
- Thinking Import-Csv deletes files
- Assuming Import-Csv converts to JSON
data.csv?Solution
Step 1: Identify correct Export-Csv syntax
The correct syntax uses-Pathto specify the file and-InputObjectfor the objects to export.Step 2: Eliminate incorrect options
Import-Csvis for reading, not exporting. Options A and D use wrong parameter names or order.Final Answer:
Export-Csv -Path data.csv -InputObject $objects -> Option AQuick Check:
Export-Csv uses -Path and -InputObject [OK]
- Using Import-Csv instead of Export-Csv
- Wrong parameter names like -File or -Objects
- Omitting parameter names and relying on position
users.csv with content:Name,Age Alice,30 Bob,25
What will the following PowerShell command output?
Import-Csv users.csv | Where-Object { $_.Age -gt 27 } | Select-Object -ExpandProperty NameSolution
Step 1: Import and filter CSV data
The command imports the CSV, then filters rows where Age > 27. Alice's age is 30, Bob's is 25, so only Alice passes.Step 2: Select and output the Name property
Select-Object -ExpandProperty Nameoutputs the Name value directly, so output is 'Alice'.Final Answer:
Alice -> Option DQuick Check:
Filter Age > 27 = Alice [OK]
- Confusing string output with array output
- Using -ExpandProperty incorrectly
- Assuming both names print without filtering
$data | Export-Csv -Path output.csv
But the CSV file contains extra quotes around each field. How can you fix this?
Solution
Step 1: Understand default Export-Csv behavior
By default, Export-Csv adds type information as the first line and quotes fields. The extra quotes are normal but can be controlled.Step 2: Use -NoTypeInformation to remove type info and reduce quotes
Adding-NoTypeInformationremoves the type info line and often reduces unnecessary quotes.Final Answer:
Add the parameter -NoTypeInformation to Export-Csv -> Option CQuick Check:
-NoTypeInformation removes extra info and quotes [OK]
- Trying to use non-existent -NoQuotes parameter
- Confusing Import-Csv with Export-Csv
- Using -Raw which is invalid for Export-Csv
products.csv with columns Id,Name,Price. You want to increase the price by 10% and save the updated data back to products.csv. Which script correctly does this?Solution
Step 1: Import CSV and update prices correctly
$products = Import-Csv products.csv; $products | ForEach-Object { $_.Price = [math]::Round($_.Price * 1.1, 2) }; $products | Export-Csv -Path products.csv -NoTypeInformation imports CSV into $products, then uses ForEach-Object to update each Price by multiplying by 1.1 and rounding to 2 decimals.Step 2: Export updated objects back to CSV with -NoTypeInformation
$products = Import-Csv products.csv; $products | ForEach-Object { $_.Price = [math]::Round($_.Price * 1.1, 2) }; $products | Export-Csv -Path products.csv -NoTypeInformation exports $products back to the same file with-NoTypeInformationto avoid extra lines.Step 3: Identify errors in other options
Import-Csv products.csv | ForEach-Object { $_.Price = $_.Price + 10 } | Export-Csv products.csv adds 10 instead of 10%, $products = Import-Csv products.csv; foreach ($p in $products) { $p.Price = $p.Price * 1.1 }; Export-Csv $products products.csv has wrong Export-Csv syntax, Import-Csv products.csv | ForEach-Object { $_.Price = $_.Price * 1.1 } > products.csv redirects output incorrectly.Final Answer:
$products = Import-Csv products.csv; $products | ForEach-Object { $_.Price = [math]::Round($_.Price * 1.1, 2) }; $products | Export-Csv -Path products.csv -NoTypeInformation -> Option BQuick Check:
Import, update with ForEach-Object, Export with -NoTypeInformation [OK]
- Adding fixed amount instead of percentage
- Wrong Export-Csv parameter order or syntax
- Redirecting output instead of using Export-Csv
Ready to prove it?
Pass the test and this pattern moves unknown
