Question : Remove Columns from CSV file, save results to new file

Looking for help creating a script that takes input parameters...
I want to take a file l like file1 - give a script name, input filename, output filename , and a string of column names to keep...

script.ps1 file1.csv file2.csv name,address

File1.csv ( see below )
name,fullname,phone,address

script would remove all columns that are not in the columns input, result would show:

Output
File2.csv
name,address

I had a vbscript that would do this a while back but I lost it.  That script also created the file file in excel, which would be fine for this as well.

Thank you.
Mark
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
File1.csv
name,fullname,phone,address
wsmith,"William Smith",111-111-1111,111 street
bcar,"Billy Car",222-222-2222,2222 road
dward,"David Ward",333-333-3333,3333 circle


File2.csv
name,address
wsmith,111 street
bcar,2222 road
dward,3333 circle

Answer : Remove Columns from CSV file, save results to new file


Hey Mark,

In PowerShell it can be as simple as this:


$PropertiesToKeep = name,address
Import-CSV file1.csv | Select-Object $PropertiesToKeep | Export-CSV file2.csv -NoTypeInformation


HTH

Chris
Random Solutions  
 
programming4us programming4us