# Basic working with CSV

**URL:** <https://forums.powershell.org/t/basic-working-with-csv/16879>\
**Category:** PowerShell Help\
**Created:** [June 21, 2021, 9:24am UTC](https://forums.powershell.org/t/basic-working-with-csv/16879 "2021-06-21T09:24:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shysnail](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@shysnail](https://forums.powershell.org/u/shysnail)\
**Post date:** [June 21, 2021, 9:24am UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/1 "2021-06-21T09:24:57Z")

</div>

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

```auto

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

```

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 21, 2021, 9:37am UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/2 "2021-06-21T09:37:38Z")

</div>

> [@shysnail](#):
>
> I am new to PS. I have been trying to work with CSV and need your help to correct my crude code.

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

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

```

When you do an import like this …

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

```

… the output would look like this:

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

```

and you could do something like this …

```auto
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?

---

<div class="post-metadata">

**Author:** ![shysnail](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@shysnail](https://forums.powershell.org/u/shysnail)\
**Post date:** [June 21, 2021, 11:13am UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/3 "2021-06-21T11:13:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 21, 2021, 11:34am UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/4 "2021-06-21T11:34:17Z")

</div>

> [@shysnail](#):
>
> I need to get the column data out of the csv file and use one column to rename some elements

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

```

> [@shysnail](#):
>
> and other columns to populate their properties.

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

```

> [@shysnail](#):
>
> Sometimes I also need to read the names of the elements and their properties (as arrays) and export them to CSV.

???

```auto
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. 😉

---

<div class="post-metadata">

**Author:** ![shysnail](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@shysnail](https://forums.powershell.org/u/shysnail)\
**Post date:** [June 21, 2021, 11:48am UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/5 "2021-06-21T11:48:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 21, 2021, 12:14pm UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/6 "2021-06-21T12:14:18Z")

</div>

> [@shysnail](#):
>
> 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.

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. 😉

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

> **[XY problem](https://en.wikipedia.org/wiki/XY_problem)**
>
> The XY problem is a communication problem encountered in help desk, technical support, software engineering, or customer service situations where the question is about an end user's attempted solution (Y) rather than the root problem itself (X).
> The XY problem obscures the real issues and may even introduce secondary problems that lead to miscommunication, resource mismanagement, and sub-par solutions. The solution for the support personnel is to ask probing questions as to why the information i...

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:24pm UTC](https://forums.powershell.org/t/basic-working-with-csv/16879/7 "2024-05-16T20:24:14Z")

</div>


