2

I have a variable that will be put into powershell as a string from a different program that uses powershell. Lets say the variable is "value1, value2, value3" in it's entirety. I want to save this as a csv file.

I've tried using export-csv but the output I get is

#TYPE System.String
Length
34

Is there a way I can use powershell to turn the value of the string into a csv or do I have to separate each item?

4
  • 3
    So you have on variable which is a string? "value1, value2, value3"? Also, show us how you want your csv to look like. Commented Apr 5, 2016 at 19:46
  • 2
    Show us the code you use to export your data to CSV. This is probably because a string object only contains one property which is Length. Commented Apr 5, 2016 at 19:59
  • @David he's using Export-Csv. Commented Apr 5, 2016 at 20:05
  • 2
    @Chris Kuperstein, Yes ofc, but when and how he is using Export-Csv is another question. Commented Apr 5, 2016 at 20:09

1 Answer 1

3
$string = ("value1, value2, value3").Split(',').Replace(" ","")

[PSCustomObject]@{
    'Column1'=$string[0]
    'Column2'=$string[1]
    'Column3'=$string[2]
} | Export-Csv .\file.csv -Append -Force -NoTypeInformation
Invoke-Item .\file.csv

You haven't said anything about what your columns are named, what type of information you're exporting and how you want it formatted. I worked with what you provided and that's it.

The answer to your question is also easily found in google.

Powershell - Output string to CSV and format

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

1 Comment

If there was an answer on SO that fit the bill then flagging a dupe should suffice. If you would argue this question is different I would suggest it is vague and should not have been answered. Some people frown on answering duplicate questions.

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.