Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Append CSV

Tags:

powershell

I am trying to append a CSV file. Here are the lines I am using. I wasn't able to find an append option for export-csv unfortunately. Any ideas would be helpful to get this to work.

Get-ADGroupMember "Domain Admins" | select name, samaccountname | Export-Csv c:\bin\DomainAdmins.csv

$admins = Import-Csv C:\bin\DomainAdmins.csv

foreach ($i in $admins) {Get-ADUser $i.samaccountname -properties * | select name, lastlogondate | Export-Csv c:\bin\dalogon.csv}
like image 911
user2565554 Avatar asked Jul 09 '13 17:07

user2565554


People also ask

How do I append to an existing CSV file in PowerShell?

To append the data into CSV file you need to use –Append parameter while exporting to the CSV file.

How do you append data in PowerShell?

In PowerShell, the Add-Content cmdlet is used to append data in a file. The content which needs to be appended is specified in this command. Here, -Path tells the exact location of the file, and the -Value is the text which will be appended in it.


2 Answers

The documentation suggests that there is an -append flag. The example given ends with

|  export-csv –append –path \\Archive01\Scripts\Scripts.csv

Have you tried that? It works fine for me. I'm on version 3, if that matters.

like image 189
dsolimano Avatar answered Nov 10 '22 00:11

dsolimano


-Append was introduced with PowerShell v3, it's not available in PowerShell v2 and earlier. You can work around it like this, though:

... |
    ConvertTo-Csv -NoTypeInformation |
    Select-Object -Skip 1 |
    Out-File -Append "c:\bin\dalogon.csv"
like image 22
Ansgar Wiechers Avatar answered Nov 10 '22 00:11

Ansgar Wiechers