Loading Data With Powershell

  • Is there anyway to append the csv file instead of overwriting its contents?

  • If you mean keep appending output to a CSV file, the export-csv cmdlet doesn't directly support this, however you could use the convertto-csv cmdlt and remove the header row as follows:

    get-psdrive | export-csv ./psdrives.csv -NoTypeInformation

    get-psdrive | ConvertTo-Csv -NoTypeInformation |

    foreach {$start=$true} {if ($start) {$start=$false} else {$_}} |

    Out-File .\psdrives.csv -Append -Encoding ASCII

    The code snippet above is based on a more complete solution described by Dmitry Sotnikov:

    [/url]

  • thanks for the reply. I'll take a look at that broader solution for that snippet. I actually got it working by saving my outgoing data into a variable, importing the contents of the csv file into another variable and appending the two together. My powershell scripting is quite novice so if you see any issues with this let me know 🙂

    Here is the code I used:

    $Temp = Get-SqlData $srcServer 'master' $qry

    $Last = import-csv "C:\filename.csv"

    $out = $Temp + $Last | export-csv -noTypeInformation -path "C:\filename.csv"

  • @SSC Veteran : Thanks . Its working now by using the above commands

  • jcrawf02 (12/16/2008)


    corey lawson (12/15/2008)


    That's a good article, except for a couple of things.

    Why do people still literally use commas as field separators (unless they have to, because the tool/function/utility doesn't support anything else)? Gaaaahhh! The example even uses the field separator parameter...

    And, well, it's PowerShell. I'd rather do this stuff in INTERCAL, but to each their own...

    Of course, BCP does this stuff as well...

    In search of enlightenment, what should we be using as field separators?

    How about it, Corey? What do you people should be using?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply