PowerShell : resolving error – Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

Hi All,

Greetings for the day!!!

Today, a new issue and solution ! This is common or simple issue, but since it took a bit time to solve, I thought to share it.

Issue – Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’

Details

  • I have written a PowerShell script to get all apps installed on my site collection.
  • I am preparing array objects with app values which I want to export to CSV file.
  • Below is sample code:
#preparing PowerShell array object to hold the app instance data
    $AppData += New-Object PSObject -Property @{
        'App ID' = $appTile.AppId
        'App Source' = $appTile.AppSource
        'App Status' = $appTile.AppStatus
        'App Type' = $appTile.AppType
        'App Title' = $appTile.Title -join ""
   }#$AppData

   #writing app data to CSV file
   $AppData | Out-File $allApps_CSV -Append

Issue

  • While executing the PowerShell script, I am getting the following error:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’.
At C:\Prasham\Articles\PowerShell\getallappsinstalled_V1_Kj.ps1:23 char:5

  • $AppData += New-Object PSObject -Property @{
  • ~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
fig : PowerShell : error – Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’.

Solution / Cause of an error

  • It seems like a simple solution. But from the error, it was not easily understood.
  • The cause of an issue is that the array object which we are using is not declared. We need to declare the array object as
#result array declaration
$AppData = @()

  • As I declared the array, script executed like a charm.

HAPPY SHARING !!! HAPPY LEARNING!!! HAVE a FANTASTIC TIME AHEAD !!!

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.