SLPowers
asked on
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"
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"
Hi,
pls try
$headers = "Computer SubOU BusinessRole Location Department ModelType AssetTag AssignedTo LocCode SerialNumber RoomNumber GroupMembership MachineNumber Comments Add/Remove".split(' ')
Regards
pls try
$headers = "Computer SubOU BusinessRole Location Department ModelType AssetTag AssignedTo LocCode SerialNumber RoomNumber GroupMembership MachineNumber Comments Add/Remove".split(' ')
Regards
ASKER
Thanks both of those work for $header but what about $list?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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.
Open in new window
For newline, use -split "`n".