How to enumerate list items from a CSV array
-
Sunday, June 03, 2012 6:48 PM
I have done this with import-csv using a CSV file, but I cannot sort out how to do this with a comma-separated array of values. Can anyone offer up an idea?
I need to take the values in this array
$StrArray = "value1, value2, value3" #Split on the comma <?> #And then take some action in a for.next loop (one at a time) ForEach ($StrValue in $StrArray) { Write-host StrValue[X] }
- Edited by MS Discussions Acct Sunday, June 03, 2012 6:48 PM
All Replies
-
Sunday, June 03, 2012 8:00 PM
Hi,
First of all $StrArray = "value1, value2, value3" is not an array of value but a string, an array of value will be $StrArray = "value1, value2, value3"
So this code will work:
$StrArray = "value1", "value2", "value3" ForEach ($StrValue in $StrArray) { Write-host $StrValue }
Life is short, Enjoy it now. Cyreli
- Proposed As Answer by BigteddyMicrosoft Community Contributor Monday, June 04, 2012 5:57 AM
-
Tuesday, June 05, 2012 2:42 AMModerator
Hi,
Try the below code:
$StrArray = "value1,value2,value3" $String = $StrArray -split "," ForEach ($String in $Str) { Write-host $String }Regards,
Yan Li
If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.
Yan Li
TechNet Community Support
- Edited by Yan Li_Microsoft Contingent Staff, Moderator Tuesday, June 05, 2012 2:43 AM
- Proposed As Answer by Yan Li_Microsoft Contingent Staff, Moderator Friday, June 08, 2012 1:20 AM
- Marked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Monday, June 11, 2012 1:56 AM
-
Tuesday, June 05, 2012 10:21 AM
Hi,
If you have csv file just use import-csv with Delimiter param.
import-csv file.csv -Delimiter ","
But if you have string that looks like array you can just split it
"value1,value2,value3" -split ","
If you have array of strings – no problem just split.
"value11,value12,value13", "value21,value22,value23" -split ","
- Proposed As Answer by Yan Li_Microsoft Contingent Staff, Moderator Friday, June 08, 2012 1:20 AM
- Marked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Monday, June 11, 2012 1:56 AM

