Check whether CSV header is present and read data to an array

Arosh Wijepala 161 Reputation points
2021-05-24T09:49:58.437+00:00

Hi,

I have a csv file which has below data.

99096-image.png

I want to compare the headers to a predefined value to check whether the OS exists in the csv file. If it exists, I want to read the data in that column in to an array.

Anybody can help me with how can I achieve this in PowerShell?

Thanks in advance.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Ian Xue 81 Reputation points
    2021-05-24T11:34:26.437+00:00

    Hi,

    You could try something like below

    $file = "C:\temp\file.csv"  
    $os = "Windows 10"  
    $content= Import-Csv -Path $file  
    $headers = ($content | Get-Member -MemberType NoteProperty).Name  
    if($os -in $headers){  
       $kb =  $content.$os  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.