# Import Column

**URL:** <https://forums.powershell.org/t/import-column/14175>\
**Category:** PowerShell Help\
**Created:** [April 16, 2020, 5:30am UTC](https://forums.powershell.org/t/import-column/14175 "2020-04-16T05:30:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 5:30am UTC](https://forums.powershell.org/t/import-column/14175/1 "2020-04-16T05:30:55Z")

</div>

I have a 2 CSV and I want to import 3 columns (FoundInAD,Results,Uptime) from CSV2 to CSV1 and I do not know how to do it, hoping that someone could help on this.

CSV1

| ComputerName | PatchingStyle | LastSyncTime | LastSyncResult | LastReportedStatusTime | PendingReboot | NeededCount | Compliant |
| Server1 | 7Saturday9PM | 4/15/2020 18:31 | Succeeded | 4/15/2020 19:24 | 0 | 0 | 100 |

CSV2

| ServerName | Uptime | Results | FoundInAD |
| Server1 | 241 Days; 14 Hrs; 6 Mins; 47 Secs | Up | YES |

I tried this code but it is not working, am getting .

> $csv1 = Import-Csv -Path C:\BackUp\PScripts\SDO\7Saturday9PM.csv $csv2 = Import-Csv -Path C:\BackUp\PScripts\SDO\Report.csv | Select -Property FoundInAD,Results,Uptime
> 
> $Results = foreach($item1 in $csv1){  
> foreach($item2 in $csv2){  
> [PSCustomObject]@{  
> ComputerName = $item1.ComputerName  
> PatchingStyle = $item1.PatchingStyle  
> LastSyncTime = $item1.LastSyncTime  
> LastSyncResult = $item1.LastSyncResult  
> LastReportedStatusTime = $item1.LastReportedStatusTime  
> PendingReboot = $item1.PendingReboot  
> NeededCount = $item1.NeededCount  
> InstalledOrNA = $item1.InstalledOrNA  
> FoundInAD = $item2.FoundInAD  
> Results = $item2.Results  
> Uptime = $item2.Uptime  
> }  
> }  
> }  
> $Results | Out-GridView

---

<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:** [April 16, 2020, 6:18am UTC](https://forums.powershell.org/t/import-column/14175/2 "2020-04-16T06:18:18Z")

</div>

Edel, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/).

When you post code or error messages or sample data or console output format it as code using the code tags “ **PRE** ”, please. Thanks in advance.

I assume you want to combine both CSVs based on the columns $CSV1.ComputerName and $CSV2.ServerName, right? You cannot use a nested loop for that. You have to match the current element of one of the arrays to the according one of the other array … like this maybe:

```
$csv1 = Import-Csv -Path C:\BackUp\PScripts\SDO\7Saturday9PM.csv
$csv2 = Import-Csv -Path C:\BackUp\PScripts\SDO\Report.csv | Select-Object -Property ServerName, FoundInAD, Results, Uptime

$Results = foreach ($item1 in $csv1) {
    $item2 = $csv2 | Where-Object { ($item1.ComputerName -split '\.')[0] -eq $_.ServerName }

    [PSCustomObject]@{
        ComputerName = $item1.ComputerName
        PatchingStyle = $item1.PatchingStyle
        LastSyncTime = $item1.LastSyncTime
        LastSyncResult = $item1.LastSyncResult
        LastReportedStatusTime = $item1.LastReportedStatusTime
        PendingReboot = $item1.PendingReboot
        NeededCount = $item1.NeededCount
        InstalledOrNA = $item1.InstalledOrNA
        FoundInAD = $item2.FoundInAD
        Results = $item2.Results
        Uptime = $item2.Uptime
    }
}
$Results | Out-GridView
```

---

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 6:27am UTC](https://forums.powershell.org/t/import-column/14175/3 "2020-04-16T06:27:04Z")

</div>

Hi Olaf,

Thanks for the quick response I tried the code you shared and am getting blank values for FoundInAD,Results and Uptime.

Anyways the ComputerName from csv1 is FQDN format while the server name in CSV2 is hostname only.

&nbsp;

&nbsp;

---

<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:** [April 16, 2020, 6:30am UTC](https://forums.powershell.org/t/import-column/14175/4 "2020-04-16T06:30:19Z")

</div>

> [@](#):
>
> Anyways the ComputerName from csv1 is FQDN format while the server name in CSV2 is hostname only.

I could not recognize that in your sample data. So you have to convert the FQDN to a hostname to make it comparable to the hostname from your CSV2. Do you need help with that?

---

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 6:30am UTC](https://forums.powershell.org/t/import-column/14175/5 "2020-04-16T06:30:48Z")

</div>

[quote quote=219510]Hi Olaf,

Thanks for the quick response I tried the code you shared and am getting blank values for FoundInAD,Results and Uptime.

Anyways the ComputerName from csv1 is FQDN format while the server name in CSV2 is hostname only.

[/quote]

It works fine if I change the ServerName to FQDN in csv2.

---

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 6:31am UTC](https://forums.powershell.org/t/import-column/14175/6 "2020-04-16T06:31:35Z")

</div>

[quote quote=219510]Hi Olaf,

Thanks for the quick response I tried the code you shared and am getting blank values for FoundInAD,Results and Uptime.

Anyways the ComputerName from csv1 is FQDN format while the server name in CSV2 is hostname only.

[/quote]

It works fine if I change the ServerName to FQDN in csv2, but I don’t wanna do that, is there any other way?

---

<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:** [April 16, 2020, 6:36am UTC](https://forums.powershell.org/t/import-column/14175/7 "2020-04-16T06:36:26Z")

</div>

> [@](#):
>
> It works fine if I change the ServerName to FQDN in csv2, but I don’t wanna do that, is there any other way?

I actually already updated the code I posted above. Try it. 😉

---

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 6:42am UTC](https://forums.powershell.org/t/import-column/14175/8 "2020-04-16T06:42:09Z")

</div>

Hi Olaf,

yes, if there is other way without changing the servername to FQDN… I use the hostname to find the device in AD, if I change that to FQDN the value from FoundInAD will be the issue.

---

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 6:45am UTC](https://forums.powershell.org/t/import-column/14175/9 "2020-04-16T06:45:55Z")

</div>

Thank you so much Olaf, appreciate the help… It is working fine now, been trying to work on this for more than 3 weeks. lol

---

<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:** [April 16, 2020, 7:28am UTC](https://forums.powershell.org/t/import-column/14175/10 "2020-04-16T07:28:51Z")

</div>

> [@](#):
>
> Thank you so much Olaf, appreciate the help… It is working fine now, been trying to work on this for more than 3 weeks. lol

So you should have asked this 2 weeks ago. 😉

---

<div class="post-metadata">

**Author:** ![edefer63](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@edefer63](https://forums.powershell.org/u/edefer63)\
**Post date:** [April 16, 2020, 10:33am UTC](https://forums.powershell.org/t/import-column/14175/11 "2020-04-16T10:33:20Z")

</div>

Hi Olaf,

One more favor, I did export-csv and I got the output.csv is there a way to convert this to HTML without changing the format of columns?

&nbsp;

---

<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:** [April 16, 2020, 10:38am UTC](https://forums.powershell.org/t/import-column/14175/12 "2020-04-16T10:38:48Z")

</div>

> [@](#):
>
> One more favor, I did export-csv and I got the output.csv is there a way to convert this to HTML without changing the format of columns?

Hmmm … did you try [ConvertTo-Html](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-html?view=powershell-7)?

Please read th ecomplete help including the examples to learn how to use it.

---

<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:29pm UTC](https://forums.powershell.org/t/import-column/14175/13 "2024-05-16T20:29:52Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


