0

I have a string [R1A_0001, R1A_0002, R1A_0003, R1A_0004, R1A_0005] which I need to convert into array in Powershell.

$scripts = "[R1A_0001, R1A_0002, R1A_0003, R1A_0004, R1A_0005]"
Write-Output $scripts
$scripts = $scripts -replace '[\[|\]]','"'
$scripts = $scripts -replace '[,]','","'
$scripts = $scripts -replace '[ ]',''
$scripts = @($scripts)

I would expect to get an array out of this code, but instead I get "R1A_0001","R1A_0002","R1A_0003","R1A_0004","R1A_0005" - still a string... Please help

1 Answer 1

1

You can use methods from .net string type :

PS P:\> $scripts = "[R1A_0001, R1A_0002, R1A_0003, R1A_0004, R1A_0005]"
PS P:\> $scripts = $scripts.Replace("[","").Replace("]","").Replace(" ","")
PS P:\> $scriptsCsv = $scripts.Split(",")
PS P:\> $scriptsCsv
R1A_0001
R1A_0002
R1A_0003
R1A_0004
R1A_0005
PS P:\> $scriptsCsv.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String[]                                 System.Array
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.