PowerShell Convert String Into Array

Posted on 2013-05-21
Medium Priority
Last Modified: 2013-05-21
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 =
Question by:brendan-amex
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Accepted Solution

apathy42 earned 2000 total points
ID: 39186022
$var.split(";") will turn it into the array.

Author Closing Comment

ID: 39186027
Good quick answer. Thank you!
LVL 70

Expert Comment

ID: 39186029
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

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 "; "

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.

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question