Row Count parameter in powershell RRS feed

  • Question

  • I am not a powershell expert so please help!

    I have a script which counts rows in the .csv file, I want to add "2 additional counts" to my row count for header & footer eg if the row count is 10 it should print 12 (10 + 2). I tried to add @($csv).count+2 but it didn't work. Any ideas how I can do that.

    Below is the script -

    $csv = import-csv E:\BBUser.csv
    "{0}|{1}" -f "***FileFooter", @($csv).count | Out-File -append E:\BBUser.csv

    Thanks in advance. Please let me know if you need additional information


    Monday, April 14, 2014 5:29 PM


  • Hi Kukar,

    You just need to put some parentheses around the expression to force the order of operations that you want.  By default, the comma and -f operators have a higher precedence than the + operator, so you wind up doing something like "***FileFooter|10" + 2, which results in the string "***FileFooter|102".  To do what you intend, try this:

    "{0}|{1}" -f "***FileFooter", (@($csv).count + 2) | Out-File -append E:\BBUser.csv 

    • Marked as answer by YVK Monday, April 14, 2014 5:41 PM
    Monday, April 14, 2014 5:35 PM