1

I have a value written in some file...

"v1", "v2", "v3", "v4"

If I read it as a line from the file, the entire value comes as a string. I can loop/foreach and make it a string array, but I am curious to find out if that's the only option I have? Can I have a simple conversion way in PowerShell to change the string to array, as we typically do like...

$s = @("v1", "v2", "v3", "v4")

2 Answers 2

2

try:

$myarray = (gc .\myfile.txt) -split ', '
Sign up to request clarification or add additional context in comments.

Comments

0

You could try conversion durring getting content like below

 $a=@()
 Get-content .\file.txt | %{ $a += $_.split(",")}

The problem is that always after getting line from file it will be a string

1 Comment

+= appending to an array should only be used when the array is small and/or there are few append operations. Problem is that each operation copies the entire content to a new array, so it's really slow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.