<<< Back to Search

Array

Text List into Array

This code converts lines of plain text into an array. Resulting array can be used to iterate items or modify individually

# Plain Text List
$textList = "
1a
2b

3c
4d
"

# Convert into array (split function) removing empty entries
$textArr = $textList.Split("`r`n",[System.StringSplitOptions]::RemoveEmptyEntries)

# Print into console as an array 
$textArr | Foreach-Object {
    Write-Host "Item:  $($_)"
}

PS C:\>
Item:  1a
Item:  2b
Item:  3c
Item:  4d