there are several ways to build that string. [grin] here are 4 of them that come to mind for me.
what the code does ...
- fakes reading in a CSV file with FirstName, LastName data
replace the entire #region/#endregion
block with your preferred data source.
- iterates thru the resulting collection
- builds the desired string with 4 different methods
my preference is the -f
string format operator, but many folks prefer string concatenation.
- sends each out to the display
- adds a divider line between the groups of results
the code ...
#region >>> fake reading in a CSV file
# when ready to do this for real, use your prefered data source
# and delete or comment out the entire "#region/#endregion" block
$NameList = @'
FirstName, LastName
Alfa, Bravo
Charlie, Delta
Echo, Foxtrot
'@ -split [System.Environment]::NewLine |
ConvertFrom-Csv
#endregion >>> fake reading in a CSV file
foreach ($NL_Item in $NameList)
{
# string format operator
'{0}.{1}' -f $NL_Item.FirstName[0], $NL_Item.LastName
# -join operator
$NL_Item.FirstName[0], $NL_Item.LastName -join '.'
# string concatenation
$NL_Item.FirstName[0] + '.' + $NL_Item.LastName
# variable-in-string expansion
"$($NL_Item.FirstName[0]).$($NL_Item.LastName)"
# yes, you can multiply a string [*grin*]
'=' * 20
}
output ...
A.Bravo
A.Bravo
A.Bravo
A.Bravo
====================
C.Delta
C.Delta
C.Delta
C.Delta
====================
E.Foxtrot
E.Foxtrot
E.Foxtrot
E.Foxtrot
====================