• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 660
  • Last Modified:

PowerShell Convert String Into Array

I have given up! I have a string with a semicolon ; as the delimiter - how do I use that to turn it into an array?

$var = One; Two; Three

What I have:
echo $var =
One; Two; Three

What I want:
echo $var =
One;
Two;
Three
0
brendan-amex
Asked:
brendan-amex
1 Solution
 
apathy42Commented:
$var.split(";") will turn it into the array.
0
 
brendan-amexAuthor Commented:
Good quick answer. Thank you!
0
 
QlemoC++ DeveloperCommented:
You really want the semicolon as part of the output? And what about the trailing (or leading) space? .Split or -split will both remove the delimiter. The result is
One
.Two
.Three

Open in new window

where the dot represents a space. We can use -split to have a two-char delimiter:
PS> "One; Two; Three" -split "; "
One
Two
Three

Open in new window

The .Split method is not able to do that, it accepts only arrays of single delimiting chars, which are then used separately.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now