PowerShell - CSV - Multiple Headers and Values - Foreach - Use Header and Value

LukePWilkins

I have a CSV -

title,client,tech,name,version,createdby,created
sampleTitle,MyCompany,Office365,sampleMonday,11.0,Luke Wilkins,2020-05-08T19:10:08Z

I'd like to enumerate through that CSV and use both the header and the value. Is there a way without specifying $variable.title, $variable.client etc

cheers

Doug Maurer

You have some options depending on how you want to use them. I'm going to expand your example to help all of us.

Create the sample file

$tempfile = New-TemporaryFile

@'
title,client,tech,name,version,createdby,created
sampleTitle1,MyCompany1,Office365,sampleMonday,11.0,Luke Wilkins,2020-05-08T19:10:08Z
sampleTitle2,MyCompany2,Office365,sampleMonday,12.0,Luke Wilkins,2020-05-08T19:10:08Z
'@ | Out-File $tempfile -Encoding utf8

Import the CSV

$csvdata = Import-Csv $tempfile

To simply get each header/value this is one way.

$csvdata | foreach {
    $_.psobject.properties | foreach {
        "Name: {0}`nValue: {1}" -f $_.name,$_.value
    }
}

Which shows (trimmed)

Name: title
Value: sampleTitle1
Name: client
Value: MyCompany1
Name: tech
Value: Office365
Name: name
Value: sampleMonday
Name: version
Value: 11.0
Name: createdby
Value: Luke Wilkins
Name: created
Value: 2020-05-08T19:10:08Z

Something I frequently do is create custom objects with the headers as the properties.

$csvdata | foreach {
    $ht = [ordered]@{}
    $_.psobject.properties | foreach {$ht += @{$_.name = $_.value}}
    [pscustomobject]$ht
} -OutVariable hashtable

Which shows and stores the following output

title     : sampleTitle1
client    : MyCompany1
tech      : Office365
name      : sampleMonday
version   : 11.0
createdby : Luke Wilkins
created   : 2020-05-08T19:10:08Z

title     : sampleTitle2
client    : MyCompany2
tech      : Office365
name      : sampleMonday
version   : 12.0
createdby : Luke Wilkins
created   : 2020-05-08T19:10:08Z

And if you need to create a property list out of the column headers you can use

$props = $csvdata | select -First 1 | foreach {$_.psobject.properties.name}

or

$props = (Get-Content $tempfile -TotalCount 1).split(',')

And we can use the properties to access the data in the hashtable variable like this.

$props |  foreach {$hashtable.$_}

Or by using the property index

1..3 | foreach {$hashtable.($props[$_])}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add multiple values to headers in Powershell

Powershell is it possible to store csv headers and value in hashtable

Powershell .csv foreach row add same value

Powershell Import-CSV: either reorder columns with no headers or export-CSV with no header

PowerShell - Add variables with multiple values to CSV column

PHP - Use multiple values in foreach loop

Adding values from multiple computers with custom headers in a single csv

C# Use CSV headers for assigning values to different variables

Powershell - use CSV to create multiple text files

Read CSV with multiple Headers

export to csv powershell script using multiple foreach statements

Powershell - CSV - Header - Save

CSV/Powershell Add an -Header

pandas single column value to multiple column headers with formatted values

How to use multiple values to compare in powershell

Multiple values on HTTP headers

Adding a header row with values for each column to multiple CSV files

How to convert json to csv with single header and multiple values?

PowerShell Export-Csv No Headers

Powershell export-csv with no headers?

Python: Script to compare a header and a value of a .csv, update values

Zuul Proxy CORS header contains multiple values, headers repeated twice - Java Spring Boot CORS filter config

ForEach with Multiple Variables - Powershell

How to paste all values (strings) in one data column with all other headers, conditioned on the combination of value and header

Use PowerShell -Pattern to search for multiple values on multiple files

Powershell JSON pipeline expand multiple values into one column csv

PowerShell Import-Csv: extract multiple values from a single cell

PowerShell: Find unique values from multiple CSV files

xlst query to have grouping to only show 1 header for each value of select instead of multiple headers