Basic working with CSV

I am new to PS. I have been trying to work with CSV and need your help to correct my crude code.


Header1           Header2
-----------       ------------
text1                 textA
text2                 textB
text3                 textC
$inputs = Import-csv -Path "C:\Users\Me\Desktop\Input.csv"
foreach{$input in $inputs)
{
   $Array1 = $item.Header1
   $Array2 = $item.Header2
}

#Add some items to arrays [Need correction]
$Array1 += text4, text5
$Array2 += textD, textE

#Display data in a Table format [Need correction]

Out-GridView $Array1, $Array2 

#Export to CSV [Need correction]

$data = $Array1, $Array2

$data | Export-Csv -Path "C:\Users\Me\Desktop\Output.csv" -NoTypeInformation

It’s not clear what you’re actually trying to do. Usually a CSV file content would look like this:

"Header1","Header2"
"text1","textA"
"text2","textB"
"text3","textC"

When you do an import like this …

Import-csv -Path "C:\Users\Me\Desktop\Input.csv"

… the output would look like this:

Header1 Header2
------- -------
text1   textA
text2   textB
text3   textC

and you could do something like this …

Import-csv -Path "C:\Users\Me\Desktop\Input.csv" | Out-GridView

You cannot take 2 independent and unrelated arrays and merge them magically together like you tried. What is it actually what you’re trying to do?

1 Like

I need to get the column data out of the csv file and use one column to rename some elements and other columns to populate their properties. Sometimes I also need to read the names of the elements and their properties (as arrays) and export them to CSV.

Import-csv -Path "C:\Users\Me\Desktop\Input.csv" | 
    Select-Object -Property Header1
Import-csv -Path "C:\Users\Me\Desktop\Input.csv" | 
    Select-Object -Property Header2

???

Import-csv -Path "C:\Users\Me\Desktop\Input.csv"  | 
    Get-member -MemberType 'NoteProperty' | 
        Select-Object -ExpandProperty 'Name'

… something like this?

I’m still not sure if I got what you mean. You may describe it using an example using the CSV data you shared in your first question. :wink:

1 Like

My PS is connected to a document management software where I want to bulk modify files/folders and their properties by exporting/importing Excel files. I’m trying with CSV for now.
I don’t know if it’s the right approach but instead of directly piping the data from imported CSV, I wanted to have better control over each column of data (as an array) and then pass it further to bulk modify files in my integrated software.

Anyway, I’ll study more on all the commands and find a better way to achieve the same.

While PowerShell works with objects and their properties you usually have a rich experience using these objects and properties directly. Most of the time you export those objects to a flat text file you loose some of the functionality. :wink:

Regardless of that … the way you ask is a kind of

You may search for your actual issue … not for the way you think you should solve this issue. :wink: :wink:

1 Like