Update CSV column by input from another csv

Hello,

I have CSV1 (as below) where I have to populate the last column from CSV 2 given below…

CSV1

[pre]
Computer Product Code Country Department LaptopDesktop


Com1 EliteDesk 705 HP2190 AU FN
Com2 EliteBook 830 HP1023 AU IT
Com3 EliteBook 830 HP1023 FR FN
Com4 Zbook 15U HP2020 IN FN
Com5 OptiPlex 3010 DL1721 FR FN
[/pre]

CSV2
[pre]
Product Type Code


EliteBook 1030 L HP1020
EliteBook 1040 L HP1021
EliteBook 830 L HP1023
Zbook 15U L HP2020
EliteDesk 800 D HP3035
EliteDesk 705 D HP2190
Thinkpad L480 L LE990
OptiPlex 3010 D DL1721
[/pre]

with below code, I’m unable to update ‘LaptopDesktop’ column in CSV1 whereas output gets appended with -Append parameter or without that, it just overwrite the whole CSV…How do I fix this?
[pre]
function Update-SystemType {
{
$SystemData =@()
$Productinfo = Import-Csv “C:\CSV2.csv”
$Mastercsv = Import-Csv “C:\CSV1.csv”

foreach($record in $Mastercsv)
{
$mcode = $($record.code)
$mDLtype = $($record.LaptopDesktop)

$SysType = ($Productinfo | where {$_.code -eq $mcode}).type
if ($SysType -eq $null)
{
$sysType = ‘Unknown, due to non-matching code’
}

$AddsysType=New-Object PSCustomObject
Add-Member -InputObject $AddsysType -membertype noteproperty -name “LaptopDesktop” -value $sysType
$SystemData +=$AddsysType

}Return, $SystemData
}
[/pre]

Try this…

https://gist.github.com/kpatnayakuni/ba1d1ec656a7de85d41d6bc31f9e9210

[quote quote=135426]Try this…

<script src=“https://gist.github.com/kpatnayakuni/ba1d1ec656a7de85d41d6bc31f9e9210.js”></script><link rel=“stylesheet” href=“https://github.githubassets.com/assets/gist-embed-d7f32d1731d95b9cb4d3c880c428c9e9.css”>

function Update-SystemType
{
$Mastercsv = Import-Csv "C:\Windows\Temp\CSV1.csv"
$Productinfo = Import-Csv "C:\Windows\Temp\CSV2.csv"
foreach($record in $Mastercsv)
{
$SysType = ($Productinfo | where {$_.Code -eq $record.code}).type
if ([string]::IsNullOrEmpty($SysType))
{
$sysType = 'Unknown, due to non-matching code'
}
$record.LaptopDesktop = $SysType
}
Return, $Mastercsv
}
<#
PS C:\> Update-SystemType | Format-Table
Computer Product Code Country Department LaptopDesktop
-------- ------- ---- ------- ---------- -------------
Com1 EliteDesk 705 HP2190 AU FN D
Com2 EliteBook 830 HP1023 AU IT L
Com3 EliteBook 830 HP1023 FR FN L
Com4 Zbook 15U HP2020 IN FN L
Com5 OptiPlex 3010 DL1721 FR FN D
#>
Update-SystemType | ConvertTo-Csv | Out-File -FilePath "C:\Windows\Temp\CSV3.csv"
  </div>
  <div class="gist-meta">
    <a href="https://gist.github.com/kpatnayakuni/ba1d1ec656a7de85d41d6bc31f9e9210/raw/4b9ec0abdc72862e6eda05334d0aee32a0954659/Demo-CSVMapping.ps1" style="float:right">view raw</a>
    <a href="https://gist.github.com/kpatnayakuni/ba1d1ec656a7de85d41d6bc31f9e9210#file-demo-csvmapping-ps1">Demo-CSVMapping.ps1</a>
    hosted with <img class="emoji" alt="&#x2764;" src="https://s.w.org/images/core/emoji/11/svg/2764.svg" /> by <a href="https://github.com">GitHub</a>
  </div>
</div>
<script>jQuery(document).ready(function(){jQuery("td.line-numbers").remove();});</script>[/quote]

Thanks a ton Kiran…It worked perfectly… :slight_smile: