简体   繁体   中英

Using Powershell to Export-Csv

below is the code I am using to find duplicates in a CSV and total them up. I cant figure out how to Export-CSV correctly

the code below shows what I need in the terminal window

any help would be much appreciated

 Import-Csv (Get-ChildItem "E:\Bill3\output.csv") |         
 Group-Object -Property Code |
    Select-Object -Property @{Name='Code';Expression={$_.Name}}, 
        @{Name='Quantity';Expression={
            ($_.Group| Measure-Object -Sum -Property Quantity ).Sum
          
           
        }       
        }

this is how the terminal looks, this is what i would like my exported csv to look like

在此处输入图像描述

You can do the following:

Import-Csv (Get-ChildItem "E:\Bill3\output.csv") |         
 Group-Object -Property Code |
    Select-Object -Property @{Name='Code';Expression={$_.Name}}, 
        @{Name='Quantity';Expression={
            ($_.Group| Measure-Object -Sum -Property Quantity ).Sum 
        }       
    } | Export-Csv file.csv -NoType

an empty pipeline element is not allowed

The reason you received the error message above is because of syntax issues with line continuation. The closing } does not continue the line. Therefore, you cannot add your | Export-Csv | Export-Csv command on the next line following the last } . They must be on the same line. You can simply run the following code to generate your same error:

PS > | export-csv o.csv
At line:1 char:1
+ | export-csv o.csv
+ ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

See Natural Line Continuators for an explanation on line continuation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM