Hello,
I am feeding and imported csv to a ForEach-Object loop and am confused about the result.
Each object in the collection has a property called ‘LAN Hostnames’. I need to remove everything after the first comma in this properties value on each object.
Example Objects:
Device Type: Windows 10
OS: Windows
LAN Hostname: name1, name1, name1
OS Detail: Windows NT; Windows NT; Windows NT
Device Type: Windows 10
OS: Windows
LAN Hostname: name2, name2, name2, name2, name2
OS Detail: Windows NT; Windows NT; Windows NT
I am trying to end up with:
Device Type: Windows 10
OS: Windows
LAN Hostname: name1
OS Detail: Windows NT
Here is something I have tried…
$import = Import-CSV -path E:\folder\file.csv $split = ForEach ($i in $import) { $_.'LAN Hostnames'.Split(',')[0] }
I thought this would iterate through the collection of objects and split the ‘LAN Hostnames’ value at the comma. Which it does, but $split contains only these values. $split is…
name1
name2
…etc. I am excited to learn about working with strings in this way. Any advice appreciated.