I have an array of full names,
$doctors = @(
'John Q. Smith',
'Mary X. Jones',
'Thomas L. White',
"Sonia M. O'Toole"
)
I would like to pass in to a variable the lastname only from that field. Or maybe only firstinitiallastname. Here is what I currently have giving me the firstnamelastinitial:
try {
# add firstnames to list
$firstnames = New-Object System.Collections.ArrayList
foreach ($doctor in $doctors) {
$docname = ($doctor -split '\s')
$docname = $docname[0]+$docname[-1][0]
$firstnames += $docname
}
Again, I would like to see only the last name. How do I adjust this code for that?