Solved

PowerShell Convert String Into Array

Posted on 2013-05-21
3
639 Views
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 =
One;
Two;
Three
0
Comment
Question by:brendan-amex
3 Comments
 
LVL 11

Accepted Solution

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

Author Closing Comment

by:brendan-amex
ID: 39186027
Good quick answer. Thank you!
0
 
LVL 68

Expert Comment

by:Qlemo
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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now