PowerShell convert a text to array

I have no doubt this will be ridiculously simple for most and I hate asking.

Have always used a workaround for the bottom 2 scenarios that only involve an extra line or 2 but I also know there is a better way and frankly my way has become embarrassing.  I am sure in uses the @ and or split but can’t make it work.
How can I make the $List and $header into an array for either of the 2 below scenarios without my stupid workarounds where I pipe them into temp text files.

I want to run actions on each word in the below examples but it obviously treats the $List and $Header as one string.  
 
Foreach ($word in $List) {
}
# Or
Foreach ($word in $Header) {
}

Thanks



$List = "Computer
SubOU
BusinessRole
Location
Department
ModelType
AssetTag
AssignedTo
LocCode
SerialNumber
RoomNumber
GroupMembership
MachineNumber
Comments
Add/Remove"

 $header = "Computer SubOU BusinessRole Location Department ModelType AssetTag AssignedTo LocCode SerialNumber RoomNumber GroupMembership MachineNumber Comments Add/Remove"
SLPowersEngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You can use an array from start, or split the text on space / newline
$header = "Computer", "SubOU","BusinessRole" # and so on
$header = "Computer SubOU BusinessRole Location Department ModelType AssetTag AssignedTo LocCode SerialNumber RoomNumber GroupMembership MachineNumber Comments Add/Remove" -split ' '

Open in new window

For newline, use -split "`n".
0
Rgonzo1971Commented:
Hi,

pls try

$headers = "Computer SubOU BusinessRole Location Department ModelType AssetTag AssignedTo LocCode SerialNumber RoomNumber GroupMembership MachineNumber Comments Add/Remove".split(' ')

Regards
0
SLPowersEngineerAuthor Commented:
Thanks both of those work for $header but what about $list?
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
For $list you have to use the newline, as mentioned!
$List = "Computer
SubOU
BusinessRole
Location
Department
ModelType
AssetTag
AssignedTo
LocCode
SerialNumber
RoomNumber
GroupMembership
MachineNumber
Comments
Add/Remove" -split "`n"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rainer JeschorCommented:
Or just an empty string:
$Header = "Computer SubOU BusinessRole Location Department ModelType AssetTag AssignedTo LocCode SerialNumber RoomNumber GroupMembership MachineNumber Comments Add/Remove" 
$headArray = $header.Split(" ")

$List = "Computer
 SubOU
 BusinessRole
 Location
 Department
 ModelType
 AssetTag
 AssignedTo
 LocCode
 SerialNumber
 RoomNumber
 GroupMembership
 MachineNumber
 Comments
 Add/Remove"

 [string]$sep = ""
 Foreach ($word in $List.Split($sep,[StringSplitOptions]::RemoveEmptyEntries)) {
    Write-Host "New list word $word"
 }

 # Or
 Foreach ($word in $Header.Split(" ")) {
    Write-Host "New word $word"
 }

Open in new window

0
SLPowersEngineerAuthor Commented:
Thanks so much.

After being provided the -split option i looked deeper into it and discovered this.

-split '[\n]'

That seems to work the the $List string.

I appreciate the guidance and have a great day.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, that works too, but it is a regular expression then. Overcomplicated. You use a regex only if you need some kind of condition (e.g. phrases, or combination of delimiters) to determine where to split.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.