# powershell add item to psobject

**URL:** <https://forums.powershell.org/t/powershell-add-item-to-psobject/7949>\
**Category:** PowerShell Help\
**Created:** [January 30, 2017, 7:32am UTC](https://forums.powershell.org/t/powershell-add-item-to-psobject/7949 "2017-01-30T07:32:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwes](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jwes](https://forums.powershell.org/u/jwes)\
**Post date:** [January 30, 2017, 7:32am UTC](https://forums.powershell.org/t/powershell-add-item-to-psobject/7949/1 "2017-01-30T07:32:59Z")

</div>

When executing the script, I get an error in the command “$CSV += $NieuwePC”  
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op\_Addition’.  
I do not understand what this message meant and how to solve it.

$PATH = ‘N:\ApplicatieGebruik\applicatie gebruik.csv’  
$CSV = Import-Csv $PATH  
$Datum = get-date -format g

$Computer = $CSV | where {$\_.Werkplek -eq “$env:computername”}  
if($Computer) # computer bestaat  
{  
foreach($row in $CSV)  
{  
if ($row.Werkplek -eq “$env:computername”)  
{  
“gevonden”  
$row.Datum = $Datum  
$row.Gebruiker = $env:username  
}  
}  
}  
else # computer bestaat niet  
{  
$NieuwePC = [pscustomobject] @{Werkplek = “$env:computername”; datum = “$Datum”; Gebruiker = “$env:username”}  
$CSV += $NieuwePC  
“werkplek toevoegen”  
}

$CSV | Sort-Object { $\_.Datum -as [datetime]} -Descending | Export-Csv $PATH -NoTypeInformation

---

<div class="post-metadata">

**Author:** ![paul-frankovich](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@paul-frankovich](https://forums.powershell.org/u/paul-frankovich)\
**Post date:** [January 30, 2017, 8:02am UTC](https://forums.powershell.org/t/powershell-add-item-to-psobject/7949/2 "2017-01-30T08:02:12Z")

</div>

From what I can tell, you are trying to add a new PC to a CSV if it doesn’t exist or update an existing record if it does. The error is because the PSCustomObject doesn’t have an add method. I would write this as a function and let the pipeline take care of adding it the the CSV.

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [January 30, 2017, 8:11am UTC](https://forums.powershell.org/t/powershell-add-item-to-psobject/7949/3 "2017-01-30T08:11:26Z")

</div>

If you replace below line

$CSV = Import-Csv $PATH

with

$CSV = [System.Collections.Generic.List[PSCustomObject]](Import-Csv $PATH)

it should work.

---

<div class="post-metadata">

**Author:** ![jwes](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jwes](https://forums.powershell.org/u/jwes)\
**Post date:** [January 30, 2017, 9:14am UTC](https://forums.powershell.org/t/powershell-add-item-to-psobject/7949/4 "2017-01-30T09:14:36Z")

</div>

Thank you very much Daniel  
works like a charm.  
I’m happy.

---

<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:39pm UTC](https://forums.powershell.org/t/powershell-add-item-to-psobject/7949/5 "2024-05-16T20:39:34Z")

</div>


