Drawing specific information from a file

Welcome Forums General PowerShell Q&A Drawing specific information from a file

Viewing 2 reply threads
  • Author
    Posts
    • #205623
      Participant
      Topics: 12
      Replies: 11
      Points: 156
      Rank: Participant

      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

       

       

       

       

    • #205629
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      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.

      • This reply was modified 3 months, 1 week ago by Olaf.
      • This reply was modified 3 months, 1 week ago by Olaf.
    • #205755
      Participant
      Topics: 5
      Replies: 322
      Points: 445
      Helping Hand
      Rank: Contributor
      # Convert string object to object array
      $data = @"
      Name, Country
      Kevin, UK
      George, Sweden
      Andrew, Germany
      "@ | ConvertFrom-Csv
      
      # Filter names
      $data | Select-Object Name
      
Viewing 2 reply threads
  • The topic ‘Drawing specific information from a file’ is closed to new replies.