3

I've given a simple example below. Does anyone know what I have to do to output the string into two columns? My searching has not returned much in the way of formatting outputs into CSV. Please point me in the right direction!

$LogFile = c:\somefile.csv
"Hello World" | Out-File $LogFile
4
  • Add ; instead of space. Commented Feb 18, 2015 at 17:37
  • Thanks for replying. It doesn't work for me. I also tried separating into different "" as well as the same. Commented Feb 18, 2015 at 18:00
  • You need to explain where the columns are coming from. e.g. In your example would each word be in its own column? Commented Feb 18, 2015 at 18:55
  • Hi there, sorry for the unsuitable example. Basically I will have two variables, both strings that need inserting into separate columns in a CSV file. So $A in one column and $B in another. Hope that helps! Commented Feb 18, 2015 at 19:17

2 Answers 2

4

Reading your comments which supplement your question, creating a PSObject around the variables you are trying to export to CSV might give you more control over your output. Consider the following:

$A = "Hello";
$B = "World";

$wrapper = New-Object PSObject -Property @{ FirstColumn = $A; SecondColumn = $B }
Export-Csv -InputObject $wrapper -Path C:\temp\myoutput.txt -NoTypeInformation

Creates the file C:\temp\myoutput.txt with two columns (FirstColumn and SecondColumn) and the variables $A and $B placed in those columns in the first row

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, that's great! Tell me one more thing if you know. How is the column order decided? I changed column one to "File" and column two to "Result", but column two appears first?
Unfortunately the order of a Powershell hashtable (which is what you are passing to the -Property argument) is not guaranteed, so you can't control the order of the properties on the PSObject. If you don't mind it getting ugly, you can work around it by doing -InputObject ($wrapper | select File, Result).
Its no real problem. Your answer still suits my purpose. Thanks for your help and the extra info! :)
Good stuff and no problem :-)
You can control the column order: $wrapper | Select-Object FirstColumn, SecondColumn | Export-CSV C:\temp\myoutput.txt -NoTypeInfo
|
3

To be honest with you I think your example is probably too trivial. For the example that you have though:

'Hello World'.Replace(' ',',') | Out-File $LogFile

This will of course give a faulty result if there are spaces that you want to keep in the data. Hence my expectation that your example is too trivial.

1 Comment

Have to say I like this method better. Yes it takes a bit more thought upfront on how your file will look but gives you so much more freedom!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.