0
votes

I'm having a problem with Powershell. I need to import a csv file, format it and export it.

Now one of the fields is the same value on every line so I want to name the export file the same as a cell value.

I am importing the csv with

    Import-Csv c:\tmp\200114.csv

Formatting the output with

    Select-Object @{expression={$_.code}; label='ID NUMBER'} etc etc but one of the fields being DESCRIPTION

Then exporting with:

    Export-Csv -NoTypeInformation c:\tmp\test1.csv

So basically I want to name the file something like the following where DESCRIPTION is the Field name and the row is 1(as they are all the same):

    Export-Csv -NoTypeInformation c:\tmp\@{expression={$_.DESCRIPTION[1]};}.csv

But I just get:

Export-Csv : Cannot validate argument on parameter 'Delimiter'. The argument is null. Supply a non-null argument and tr y the command again.

Ideally I could like the file name to be:

Todays Date - DESCRIPTION Column ROW 1 Value.csv

Many thanks for any input....

SAMPLE DATA:

ID NUMBER,NAME,ADDRESS 1,ADDRESS 2,CITY,STATE,ZIP,Spare,PHONE #,DESCRIPTION,Spare,Spare,VALUE 1,Name 1,address 1,address 2,city 1,state 1,zip 1,,Phone 1,CC098-1,,,NCV 2,Name 2,address 2,address 3,city 2,state 2,zip 2,,Phone 2,CC098-1,,,NCV


Thanks, I am very grateful for this. I am struggling to get this working though. It must be to do with:

    Select @{n='ID NUMBER';e={$_.code}}, @{n='DESCRIPTION';e={...}} 

I Don't know the Select command and can't find anything on it, I can't even see how the n= & e= etc are doing. I trimmed it down to:

    $csv = Import-Csv c:\tmp\200114.csv | Select-Object @{expression={$_.code}; label='ID NUMBER'} | $filename = "$(Get-Date -f 'yyyy-MM-dd') - $($csv[0].DESCRIPTION).csv" | $csv | Export-Csv $filename -NoTypeInformation 

but I just get errors:

Expressions are only allowed as the first element of a pipeline. At line:1 char:108

    + $csv = Import-Csv c:\tmp\200114.csv | Select-Object @{expression={$_.code}; label='ID NUMBER'} | $filename  <<<< = "$
    (Get-Date -f 'yyyy-MM-dd') - $($csv[0].DESCRIPTION).csv" | $csv | Export-Csv $filename -NoTypeInformation
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

Thanks again.

1
Please add some sample data from the CSV files.vonPryz
Sure, I've had to put dummy data in, but the headers are correct:Paul
select is an alias for Select-Object. When creating calculated properties you can use either @{name=...;expression=...} or @{label=...;expression=...}. name, label and expression can be abbreviated as n, l and e respectively.Ansgar Wiechers
I see that makes sense. Thanks.Paul

1 Answers

0
votes

Capture the processed CSV in a variable:

$csv = Import-Csv c:\tmp\200114.csv | Select @{n='ID NUMBER';e={$_.code}},
         @{n='DESCRIPTION';e={...}},
         ...

Construct your filename:

$filename = "$(Get-Date -f 'yyyy-MM-dd') - $($csv[0].DESCRIPTION).csv"

Then export the data:

$csv | Export-Csv $filename -NoTypeInformation