📖 Scenario: You work in a small office where you need to manage employee data stored in CSV files. You want to automate reading this data, filtering it, and saving the filtered data back to a new CSV file.
🎯 Goal: Build a PowerShell script that imports employee data from a CSV file, filters employees based on their department, and exports the filtered data to a new CSV file.
📋 What You'll Learn
Use Import-Csv to read data from a CSV file
Create a variable to hold the department name to filter
Use a Where-Object filter to select employees from the specified department
Use Export-Csv to save the filtered data to a new CSV file
Print the filtered employee data to the console
💡 Why This Matters
🌍 Real World
Automating CSV data handling is common in office tasks like managing employee records, sales data, or inventory lists.
💼 Career
Knowing how to import, filter, and export CSV files with PowerShell is useful for system administrators, data analysts, and IT support professionals.
Progress0 / 4 steps
1
Import employee data from CSV
Write a line of PowerShell code to import employee data from a CSV file named employees.csv into a variable called employees using Import-Csv.
PowerShell
Hint
Use Import-Csv with the -Path parameter to read the CSV file.
2
Set department filter
Create a variable called departmentFilter and set it to the string Sales to specify the department you want to filter.
PowerShell
Hint
Assign the string 'Sales' to the variable departmentFilter.
3
Filter employees by department
Use Where-Object to filter employees where the Department property equals departmentFilter. Store the result in a variable called filteredEmployees.
PowerShell
Hint
Use the pipeline | and Where-Object with a script block to compare Department to departmentFilter.
4
Export filtered data and display output
Export filteredEmployees to a CSV file named sales_employees.csv using Export-Csv with -NoTypeInformation. Then print filteredEmployees to the console.
PowerShell
Hint
Use Export-Csv with -NoTypeInformation to save the filtered data. Then output the variable to display it.
Practice
(1/5)
1. What does the PowerShell cmdlet Import-Csv do?
easy
A. Reads a CSV file and converts each row into an object
B. Saves objects into a CSV file
C. Deletes a CSV file from disk
D. Converts objects into JSON format
Solution
Step 1: Understand the purpose of Import-Csv
Import-Csv reads 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-Csv saves objects to CSV, deletion and JSON conversion are unrelated to Import-Csv.
Final Answer:
Reads a CSV file and converts each row into an object -> Option A
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 Name outputs the Name value directly, so output is 'Alice'.
Final Answer:
Alice -> Option D
Quick Check:
Filter Age > 27 = Alice [OK]
Hint: Filter with Where-Object, then expand property for direct output [OK]
Common Mistakes:
Confusing string output with array output
Using -ExpandProperty incorrectly
Assuming both names print without filtering
4. You run this command to export data:
$data | Export-Csv -Path output.csv
But the CSV file contains extra quotes around each field. How can you fix this?
medium
A. Use Import-Csv instead of Export-Csv
B. Add the parameter -NoQuotes to Export-Csv
C. Add the parameter -NoTypeInformation to Export-Csv
D. Use Export-Csv -Raw to avoid quotes
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 -NoTypeInformation removes the type info line and often reduces unnecessary quotes.
Final Answer:
Add the parameter -NoTypeInformation to Export-Csv -> Option C
Quick Check:
-NoTypeInformation removes extra info and quotes [OK]
Hint: Use -NoTypeInformation to clean Export-Csv output [OK]
Common Mistakes:
Trying to use non-existent -NoQuotes parameter
Confusing Import-Csv with Export-Csv
Using -Raw which is invalid for Export-Csv
5. You have a CSV file 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?
$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 -NoTypeInformation to avoid extra lines.