0
votes
#Create an empty System.Array object
$employees = @()


#Add newly created object to the array
#$employees += $employee
for ($i=1;$i -le 2;$i++)
{
    $employee = New-Object System.Object
    $employee | Add-Member -MemberType NoteProperty -Name "Creation Time" -Value $(Get-date)
    $employee | Add-Member -MemberType NoteProperty -Name "Employee ID" -Value $($i.ToString("D2"))
    $employees += $employee
    Start-Sleep -Seconds 1
}


#Finally, use Export-Csv to export the data to a csv file
$employees | Export-Csv -NoTypeInformation -Path "c:\temp\test\EmployeeList.csv"

this is code write data is overwrite old data,i want to same change code, add "-Append"

$employees | Export-Csv  -NoTypeInformation -Path "c:\temp\test\EmployeeList.csv" -Append 

error message:

Export-Csv : A parameter cannot be found that matches parameter name 'Append'. At D:\powershell\MU_TAG_ET.ps1:292 char:70 + $employees | Export-Csv -Path "c:\temp\test\EmployeeList.csv" -Append <<<<
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand

can u help me??

2
-append is starting from 3.0 version. Are you in powershell 2.0?CB.

2 Answers

0
votes

If you don't have Powershell V3 or later as mentioned, you could test to see if your CSV exists, if that's the case, use Import-Csv to $employees array, add new $employee PSCustomObjects, and export when done.

0
votes

With PS 5.1 "- Append" is available.

$employees | Export-Csv -NoTypeInformation -Append -Path "c:\temp\test\EmployeeList.csv"

(P.s. Can an admin correct the spelling in the original question please?)

BTW, I see a "- Force" option. I am not sure why that is needed but you may hae to enforce the append. Try it and let us know.... -Patrick Burwell, www.burwell.tech