0phoi5 Posted December 15, 2017 Share Posted December 15, 2017 (edited) 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 December 15, 2017 by haze1434 Quote Link to comment Share on other sites More sharing options...
RazerBlade Posted December 15, 2017 Share Posted December 15, 2017 can't you just use python? Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 18, 2017 Author Share Posted December 18, 2017 On 15/12/2017 at 3:54 PM, RazerBlade said: can't you just use python? Unfortunately not, this is part of a much larger tool that is PowerShell based. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted December 18, 2017 Share Posted December 18, 2017 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 Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 18, 2017 Author Share Posted December 18, 2017 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. Quote Link to comment Share on other sites More sharing options...
PoSHMagiC0de Posted December 18, 2017 Share Posted December 18, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.