Drawing specific information from a file

Hello Everyone,

This might seem a simple question however I am just trying to get my head around this. Say for example if I have a file with headers in it like names and country seperated by breaks in it and I want to filter and output just the name header

Name, Country

Kevin, UK

George, Sweden

Andrew, Germany

 

I could use a $a= Get-Content C:\temp\names.txt to pull the information from the file. Then I could use a a for each loop to trawl the file. However how does one only filter and output the specific data under the single column heading ?.

Thanks

 

 

 

 

If your files are valid CSV files you should treat them as such and use Import-Csv instead of Get-Content.

Please read the complete help including the examples to learn how to use the cmdlets you’re about to use.

Regardless of that - when you post code please format this as code using the code tag button named “PRE”. Example data should be formatted as code as well please.

# Convert string object to object array
$data = @"
Name, Country
Kevin, UK
George, Sweden
Andrew, Germany
"@ | ConvertFrom-Csv

# Filter names
$data | Select-Object Name