Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:29:24+00:00 2026-06-09T21:29:24+00:00

I was following along with a book about WMI and Powershell, and it explains

  • 0

I was following along with a book about WMI and Powershell, and it explains how to create a custom class in Powershell. It involves writing some C# code, and the add-type command. When I try to create multiple entries in the object with the += command, I get the following error:

Method invocation failed because [pawobject] doesn't contain a method named 'op_Addition'.
At line:23 char:14
+ $MyObjects += <<<<  $MyObject
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

The code I am using to create the class is this:

$source = @"
public class pawobject
{
    public string Description { get; set; }
    public string Name { get; set; }
    public int Number { get; set;}
}
"@

And to add it to PowerShell I use the following:

Add-Type $source -Language CSharpVersion3

The code to create a value is this:

$MyObject = New-Object -TypeName pawobject -Property @{
    Name = "MyObject5";
    Number = 200;
    Description = "Take3"
}

This creates a single $MyObject. From here is where my problems come. I tried to create the object with multiple entries like this:

$MyObjects += $MyObject

But I get the above error. I have tried

$MyObjects = $MyObjects + $MyObject

I looked around online, and tried adding the following code the the public class:

public static string Add(string a, string b) {
    return (a + b);
}

But that seems to only work with numbers. I am not familiar at all with C#, so I’m not even sure where to start looking to fix this error. I can see this being very useful, as I am always creating new objects in my environment. Being able to define our own types would be a tremendous advantage, especially being able to control what type of data can get put into each property. Thank you in advance, have a great day!

Edit: Let me explain what I want the results to do.

I want to be able to declare something like this this:

$MyObject = New-Object -TypeName pawobject -Property @{
    Name = "MyObject";
    Number = 100;
    Description = "Take1"
    }

When I take the output of $MyObject I end up with this:

Description   Name         Number
-----------   ----         ------
Take1         MyObject     100

I would like to then be able to declare this:

$MyObjects += $MyObjects

And end up with $MyObjects having this:

Description   Name         Number
-----------   ----         ------
Take1         MyObject     100
Take2         MyObject5    200

Basically adding (not sure of the technical term for adding two sets of objects) the $MyObject to $MyObjects

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:29:26+00:00Added an answer on June 9, 2026 at 9:29 pm

    It’s interpretting it as adding two objects together
    I think in c# you would need to implement the operate like this:

        public static pawobject operator +(pawobject A, pawobject B)
        {
    
            //I have no idea how you would add two of these together
        }
    

    I’m not sure that this is actually what you want to do though. It seems like you want to create an array of objects.

    I’ve tested this out and it does create an array of objects.

    PS C:\Users\James> $source = @"
    >> public class pawobject3
    >> {
    >>     public string Description { get; set; }
    >>     public string Name { get; set; }
    >>     public int Number { get; set;}
    >>     public static pawobject3[] operator +(pawobject3 A, pawobject3 B)
    >>     {
    >>         pawobject3[] Result = new pawobject3[2];
    >>         Result[0] = A;
    >>         Result[1] = B;
    >>         return Result;
    >>     }
    >>     public static pawobject3[] operator +(pawobject3[] A, pawobject3 B)
    >>     {
    >>         pawobject3[] Result = new pawobject3[A.Length + 1];
    >>         for(int i = 0; i < A.Length; i++)
    >>             Result[i] = A[i];
    >>         Result[A.Length] = B;
    >>         return Result;
    >>     }
    >> }
    >> "@
    >>
    PS C:\Users\James> Add-Type $source -Language CSharpVersion3
    PS C:\Users\James> $MyPaw1
    PS C:\Users\James> $MyPaw1 = New-Object -TypeName pawobject3 -Property @{
    >> Name = "Test1";
    >> Number = 1;
    >> Description = "D1";
    >> }
    >>
    PS C:\Users\James> $MyPaw2 = New-Object -TypeName pawobject3 -Property @{
    >> Name = "Test2";
    >> Number = 2;
    >> Description = "D2";
    >> }
    >>
    PS C:\Users\James> $MyPaw3 = New-Object -TypeName pawobject3 -Property @{
    >> Name = "Test3";
    >> Number = 3;
    >> Description = "D3";
    >> }
    >>
    PS C:\Users\James> $MyPaws = $MyPaw1 + $MyPaw2
    PS C:\Users\James> $MyPaws += $MyPaw3
    PS C:\Users\James> $MyPaws
    
    Description                             Name                                                                     Number
    -----------                             ----                                                                     ------
    D1                                      Test1                                                                         1
    D2                                      Test2                                                                         2
    D3                                      Test3                                                                         3
    

    You need to make sure the operator knows what it’s adding together. There’s no declaration of variable type so to create the array I’m using the return type of adding 2 together.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following along with the Rails 3 in Action book, and it is
I have an HTML along the following lines: <div class=hiddenClass> // this implies display:none
I'm trying to a create an update statement along the following lines: TABLE car:
I'm reading a book about programming ASP.NET in C#. The book makes the following
I have been following along with the book Practical Django Projects. Very nice book,
I'm following along with the Liferay In Action book. I'm at the part where
I'm following along in the Yii book and am on CH5 (page 101). In
I am following along very closely with the code within the GeoCongress.us App ,
I am trying to learn WPF. I am following along with a book(WPF in
I am following along with a tutorial book on AJAX and I seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.