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?
 
QlemoBatchelor, 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
 
QlemoBatchelor, 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
SLPowersEngineerAuthor Commented:
Thanks both of those work for $header but what about $list?
0
 
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
 
QlemoBatchelor, 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.