0

Is any way to write text in last column of csv file? without ""

if ($inputID -eq $entry.HostName){

    "$inputID Ok!"
}
else{

    "$inputID doesn't exist in database!"

    $title    = 'Collector _ HostName'
    $question = 'Add new ID in csv database?'
    $choices  = '&Yes', '&No'

$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
    if ($decision -eq 0) {
    Write-Host 'confirmed'

    $ID = Read-Host -Prompt "TypeID"
    $IP = Read-Host -Prompt "TypeIP"

$wrapper = New-Object PSObject -Property @{ HostName = $ID; IP = $IP }
Export-Csv -Append -InputObject $wrapper -Path ".\test.csv" -NoTypeInformation -Force 
$dbTrimmer = Get-Content -Path ".\test.csv"
$dbTrimmer.Replace('","',",").TrimStart('"').TrimEnd('"') | Out-File ".\test.csv" -Force -Confirm:$false
    Exit
    }
    else{
    Write-Host 'No'
    Exit
    }
}

csv looks like this

ID,IP
ABC,10.10.10.10

In this moment my script can write in csv but delete an old info in csv

"FirstColumn","SecondColumn"
"ABCD","10.10.10.10"

Thank you

  • 2
    Have a look at Export-Csv – marsze Oct 25 '21 at 09:02
  • Which PowerShell version? (Please **add to the question**). Basically: [`Export-Csv -QuoteFields "FirstColumn" -InputObject $wrapper -Path C:\users\me\Desktop\test.csv`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/export-csv#example-10--export-to-csv-with-quotes-around-two-columns) – iRon Oct 25 '21 at 09:43
  • While simply removing **all** quotes in the csv _MAY_ work for this file, it can/will certainly destroy the alignment for other csv files rendering them unusable. Please read my answer to do this safely. – Theo Oct 26 '21 at 14:03

1 Answers1

0

If you need to add new rows to an existing CSV file, use the -Append switch on Export-Csv.

As for your wish to write the file without quotes around the values and headers, then bare in mind that simply trying to remove all quotes in a CSV file is risking mis-alignment in the field order and rendering the csv as unusable.

If you are using PowerShell version 7.x the you can also add parameter -UseQuotes AsNeeded

For older PowerShell versions, that is not available, but there is a safe way of doing this, see my function ConvertTo-CsvNoQuotes

Theo
  • 57,719
  • 8
  • 24
  • 41
  • I want try to remove quotes and see whats happend . it is possible? –  Oct 25 '21 at 11:37
  • @Bogdan Sure, but if any of your fields happens to contain a comma (the delimiter character) or a NewLine, you will ruin your csv. That's why I wrote the warning and gave you a link to do that properly – Theo Oct 25 '21 at 12:02
  • solved, I update my first post. Thank you. –  Oct 26 '21 at 06:29