If you really need to create a distinct variable for each row of your string, then use New-Variable in a loop:
$str = 'Company 1
Employee 1
Employee 2
Employee 3
Company 2
Employee 1
Employee 2
Employee 3'
# to know how many variables were created we will put them into array
$variables = @()
$i = 0
foreach ($row in ($str -split [Environment]::NewLine)) {
# create new variable from row's value
New-Variable -Name "row$i" -Value $row
# and create a member of array
$variables += [pscustomobject]@{Name = "row$i"; Value = $row}
$i++
}
Write-Host "Total: $i"
foreach ($var in $variables) {
Write-Host $var.Name $var.Value
}
# now we can get value from a variable
Write-Host $row1
# or from array
Write-Host $variables[1].Name $variables[1].Value
But to operate on a collection of items it is really simplier to use arrays and other collection types without creating a variable per item:
$rows = @()
foreach ($row in ($str -split [Environment]::NewLine)) {
$rows += $row
}
Write-Host "Total rows: $rows.Count"
# get the first row
$rows[0]