Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

powershell export-csv overwrite data?

$
0
0

this program is first create csv file and output date to csv file, and then for loop the data to csv file 

 

$strFileName="c:\temp\test\EMS_tag_rpt01.csv"

If (Test-Path $strFileName)

{

  # // File exists

  $rows = import-csv "c:\temp\test\EMS_tag_rpt01.csv"

  $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"

$rows+$employees | Export-Csv  -Path "c:\temp\test\EMS_tag_rpt01.csv" -NoTypeInformatio

}Else{

#Export-Csv  -Path "c:\temp\test\EMS_tag_rpt01.csv"-NoTypeInformatio

#$rows = import-csv "c:\temp\test\EMS_tag_rpt01.csv"

#echo $rows

$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

    $employees | Export-Csv  -Path "c:\temp\test\EMS_tag_rpt01.csv" -NoTypeInformatio

    $csv=Get-Content "C:\temp\test\EMS_tag_rpt01.csv"

"XXX Report,Date: $(Get-Date)" | out-File -FilePath "c:\temp\test\EMS_tag_rpt01.csv" -Encoding utf8

$csv | Out-File -FilePath "c:\temp\test\EMS_tag_rpt01.csv" -Encoding utf8 -Append

}

}

if remove this code:

    $csv=Get-Content "C:\temp\test\EMS_tag_rpt01.csv"

"XXX Report,Date: $(Get-Date)" | out-File -FilePath "c:\temp\test\EMS_tag_rpt01.csv" -Encoding utf8

$csv | Out-File -FilePath "c:\temp\test\EMS_tag_rpt01.csv" -Encoding utf8 -Append

than the export-csv file cannot overwrite data...this code is add csv file header

but add this code..second run this code,cannot write data to csv file??

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles