Bird
Raised Fist0
PowerShellscripting~20 mins

CSV operations (Import-Csv, Export-Csv) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSV Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Import-Csv command?
Given a CSV file named data.csv with the following content:
Id,Name,Age
1,Alice,30
2,Bob,25

What will be the output of this PowerShell command?
Import-Csv -Path 'data.csv' | ForEach-Object { $_.Name }
PowerShell
Import-Csv -Path 'data.csv' | ForEach-Object { $_.Name }
A
Id
Name
Age
B
Alice
Bob
C
30
25
D
1
2
Attempts:
2 left
💡 Hint
Import-Csv reads the CSV and creates objects with properties matching the headers.
💻 Command Output
intermediate
2:00remaining
What does this Export-Csv command produce?
Consider this PowerShell code:
$data = @(
  @{Id=1; Name='Alice'; Age=30},
  @{Id=2; Name='Bob'; Age=25}
)
$data | Export-Csv -Path 'output.csv' -NoTypeInformation

What will be the content of output.csv?
PowerShell
$data = @(@{Id=1; Name='Alice'; Age=30}, @{Id=2; Name='Bob'; Age=25})
$data | Export-Csv -Path 'output.csv' -NoTypeInformation
A
Id,Name,Age
1,Alice,30
2,Bob,25
B
@{Id=1; Name=Alice; Age=30}
@{Id=2; Name=Bob; Age=25}
C
"Id","Name","Age"
"1","Alice","30"
"2","Bob","25"
D
TypeId
System.Management.Automation.PSCustomObject
System.Management.Automation.PSCustomObject
Attempts:
2 left
💡 Hint
Export-Csv outputs CSV with quoted values by default and includes headers.
🔧 Debug
advanced
2:00remaining
Why does this Import-Csv command fail?
You run this command:
Import-Csv -Path 'missing.csv'

But you get an error:
Import-Csv : Cannot find path 'missing.csv' because it does not exist.
What is the cause?
PowerShell
Import-Csv -Path 'missing.csv'
AThe file 'missing.csv' does not exist in the current directory.
BImport-Csv cannot read CSV files with .csv extension.
CThe command requires -Delimiter parameter to work.
DPowerShell does not support Import-Csv on Windows.
Attempts:
2 left
💡 Hint
Check if the file path is correct and the file exists.
🚀 Application
advanced
2:00remaining
How to filter CSV rows with Import-Csv and Export-Csv?
You have a CSV file users.csv with columns: Id, Name, Age.
You want to create a new CSV adults.csv containing only rows where Age is 18 or more.
Which PowerShell command does this correctly?
AImport-Csv users.csv | ForEach-Object { if ($_.Age -ge 18) { $_ } } | Export-Csv adults.csv
BImport-Csv users.csv | Where-Object { $_.Age -lt 18 } | Export-Csv adults.csv
CGet-Content users.csv | Where { $_ -ge 18 } | Export-Csv adults.csv
DImport-Csv users.csv | Where-Object { [int]$_.Age -ge 18 } | Export-Csv adults.csv -NoTypeInformation
Attempts:
2 left
💡 Hint
Use Where-Object to filter and Export-Csv with -NoTypeInformation to save.
🧠 Conceptual
expert
2:00remaining
What is the effect of the -NoTypeInformation parameter in Export-Csv?
When using Export-Csv in PowerShell, what does the -NoTypeInformation parameter do?
AIt prevents adding a type information line at the top of the CSV file.
BIt disables exporting the header row with column names.
CIt encrypts the CSV file to protect data.
DIt converts all data to string before exporting.
Attempts:
2 left
💡 Hint
Check the first line of CSV files exported with and without this parameter.

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

  1. 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.
  2. Step 2: Compare with other options

    Export-Csv saves objects to CSV, deletion and JSON conversion are unrelated to Import-Csv.
  3. Final Answer:

    Reads a CSV file and converts each row into an object -> Option A
  4. Quick Check:

    Import-Csv = Reads CSV to objects [OK]
Hint: Import-Csv reads CSV; Export-Csv writes CSV [OK]
Common Mistakes:
  • Confusing Import-Csv with Export-Csv
  • Thinking Import-Csv deletes files
  • Assuming Import-Csv converts to JSON
2. Which of the following is the correct syntax to export objects to a CSV file named data.csv?
easy
A. Export-Csv -Path data.csv -InputObject $objects
B. Import-Csv -Path data.csv -InputObject $objects
C. Export-Csv $objects data.csv
D. Export-Csv -File data.csv -Objects $objects

Solution

  1. Step 1: Identify correct Export-Csv syntax

    The correct syntax uses -Path to specify the file and -InputObject for the objects to export.
  2. Step 2: Eliminate incorrect options

    Import-Csv is for reading, not exporting. Options A and D use wrong parameter names or order.
  3. Final Answer:

    Export-Csv -Path data.csv -InputObject $objects -> Option A
  4. Quick Check:

    Export-Csv uses -Path and -InputObject [OK]
Hint: Use -Path for file and -InputObject for data in Export-Csv [OK]
Common Mistakes:
  • Using Import-Csv instead of Export-Csv
  • Wrong parameter names like -File or -Objects
  • Omitting parameter names and relying on position
3. Given the CSV file 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 Name
medium
A. No output
B. Bob
C. Alice`nBob
D. Alice

Solution

  1. 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.
  2. Step 2: Select and output the Name property

    Select-Object -ExpandProperty Name outputs the Name value directly, so output is 'Alice'.
  3. Final Answer:

    Alice -> Option D
  4. 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

  1. 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.
  2. Step 2: Use -NoTypeInformation to remove type info and reduce quotes

    Adding -NoTypeInformation removes the type info line and often reduces unnecessary quotes.
  3. Final Answer:

    Add the parameter -NoTypeInformation to Export-Csv -> Option C
  4. 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?
hard
A. Import-Csv products.csv | ForEach-Object { $_.Price = $_.Price + 10 } | Export-Csv products.csv
B. $products = Import-Csv products.csv; $products | ForEach-Object { $_.Price = [math]::Round($_.Price * 1.1, 2) }; $products | Export-Csv -Path products.csv -NoTypeInformation
C. $products = Import-Csv products.csv; foreach ($p in $products) { $p.Price = $p.Price * 1.1 }; Export-Csv $products products.csv
D. Import-Csv products.csv | ForEach-Object { $_.Price = $_.Price * 1.1 } > products.csv

Solution

  1. 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.
  2. 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.
  3. 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.
  4. 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 B
  5. Quick Check:

    Import, update with ForEach-Object, Export with -NoTypeInformation [OK]
Hint: Use Import-Csv, update in ForEach-Object, then Export-Csv -NoTypeInformation [OK]
Common Mistakes:
  • Adding fixed amount instead of percentage
  • Wrong Export-Csv parameter order or syntax
  • Redirecting output instead of using Export-Csv
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard