I am attempting to clean up (reformat) a list of servers that has been provided to me containing the full domain name so that I can use it elsewhere. The list of servers arrive formatted like: servername.domain.com. I read the list into a variable using import-csv and remove the other columns of data not needed using the format-table command. My resulting command looks something like this:
$l=Import-Csv .\filename.csv | Format-Table TARGET
TARGET is the column name containing the server names.
When I use the following to remove the domain name portion of the server name, the resulting value is "Microsoft" repeated for each line containing the server name. I have confirmed the server names are being read into the variable correctly.
$l -replace '(.+?)\..+','$1'
The command does work if there is only one server name in the variable. I know I was able to trim the names of the servers down at one point but I think I had a brain lapse that day and just did not write down the command I used. :(
Any suggestions? My end goal is to incorporate the commands into a script for scheduled processing.
| Format-Table TARGETas it destroys the objects. You want$l=Import-Csv .\filename.csv | Select -Expand TARGETto start.