20

Is there a way to create a csv object in powershell from variable that contains string? At the moment I need to write content in a temp file and then import it to csv and I want to reduce disk operations:

$some_string | Set-Content $temp_file
$csv_content=Import-Csv $temp_file

EDIT

@Graham Gold , Јοеу thanks for the answers (I've set +1 on both of you) , but seems the Joey's answer is correct.Here's my test:

    $csv_string="
`"col1`" ; `"col2`"
val11 ; val12
val21 ; val22"

$csv_content1 = $csv_string | ConvertFrom-CSV 
$csv_content2 = $csv_string | ConvertTo-CSV 
"
ConvertFrom result:"
$csv_content1
"
ConvertTo result:"
$csv_content2

And the result:

ConvertFrom result:

H1                                                                             
--                                                                             
col1 ; "col2"                                                                  
val11 ; val12                                                                  
val21 ; val22                                                                  

ConvertTo result:
#TYPE System.String
"Length"
"47"
1
  • 1
    For a more complete and flexible solution, lease see this answer, taking a list and doing some additional magic. Commented Dec 11, 2022 at 6:23

2 Answers 2

37

You can use ConvertFrom-Csv instead:

$csv_content = $some_string | ConvertFrom-Csv -Delim ';'

Import-Csv is little more than a wrapper around Get-Content and ConvertFrom-Csv.

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

Comments

6

Even better:

$csv_content1 = ConvertFrom-CSV -delim ';' -Input @"
"col1"; "col2"
val11 ; val12
val21 ; val22
"@

3 Comments

This question has already been answered and accepted. Could you clarify why your answer is even better than one that got accepted?
Actually, on reading the question again I should have provided a slightly different solution: $csv_content1 = ConvertFrom-CSV -delim ';' -Input $some_string The advantage is that it doesn't need to put the string on the pipeline. However for the example text given when a developer want's the text embedded within the program itself, using the example with the @here string is better in that it doesn't require saving the text into a string variable first, and again create a pipeline unnecessarily.
Could you please edit your answer and add this explanation, so people that find this question in the future understand why your answer could be better? Specially when answering old questions, it is usually better to provide as many details as possible. The website works better if we help by giving information enough for people to understand how things work in our answers. Thanks.

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.