Jump to content

[PowerShell] - How to Add-Content to CSV file, ignoring new lines?


Recommended Posts

Hi all,

I'm creating a PowerShell script and am attempting to use;

"$variable1,$variable2,$variable3" | Add-Content -Path "file.csv" -Encoding UTF8

 

The variables echo/write-host as;

Variable1

Hello

Variable2

World

Variable3

Hello
World

 

My issue is that the Add-Content above should create a CSV file like this;

Hello,World,Hello World

But it doesn't. Instead, it creates;

Hello,World,Hello
 World

 

How can I go about using Add-Content to export to CSV, but get it to ignore new lines?

Opened in Excel, I want 'cell' C1 to be 'Hello World', but at the moment it's creating cell C1 as "Hello" and cell A2 (which shouldn't even exist in this case) as " World"

 

Note: I'm stuck with PowerShell version 3 unfortunately.

 

Thank you.

Edited by haze1434
Link to post
Share on other sites

Have you tried the Export-Csv cmdlet? You'll need to squeeze your variables in to an object though.

"" | select-object @{Name="Column 1"; Expression={"$variable1"}},@{Name="Column 2"; Expression={"$variable2"}}, @{Name="Column 3"; Expression={"$variable3"}} | Export-Csv -Path "test.csv" -notypeinformation -Encoding UTF8 -Append

 

Link to post
Share on other sites
2 hours ago, Jason Cooper said:

Have you tried the Export-Csv cmdlet? You'll need to squeeze your variables in to an object though.


"" | select-object @{Name="Column 1"; Expression={"$variable1"}},@{Name="Column 2"; Expression={"$variable2"}}, @{Name="Column 3"; Expression={"$variable3"}} | Export-Csv -Path "test.csv" -notypeinformation -Encoding UTF8 -Append

 

I haven't yet, no. I'll give this a go shortly, thank you.

Link to post
Share on other sites

There is a command in powershell to create a CSV file.  Issue is your variables are unnamed (no column names) so the cmdlet will not work.  If you plan on creating a CSV of items, place the things you want into a single object with names like so.

$mystuff = new-object psobject -Property @{
    variable1=$variable1
    variable2=$variable2
    variable3=$variable3
}

$mystuff | where {-not ([string]::IsNullOrEmpty($_))} | convertto-csv -notypeinformation | add-content -path "c:\yourpath\yourcsv.csv"

Now, if one of the variables is multiline then you will have issues.  You will need to add a pipe before the convertto-csv to check for this and combine the lines to make them 1 line.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...