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 4618824
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:20:35+00:00 2026-05-22T02:20:35+00:00

I am very new to PowerShell scripting and was experimenting with Function . This

  • 0

I am very new to PowerShell scripting and was experimenting with Function. This is the code I wrote:

Function Add
{
   $sum = 0;
   for($i = 0; $i -le $args.length; ++$i)
   {
     [int] $num = $args[$i]
     $sum += $num
   }
   Write-Output "Sum is $sum"
}

And I tried to call using Add 1,2,3. However, when I execute I get the following error:

Cannot convert the “System.Object[]”
value of type “System.Object[]” to
type “System.Int32”.

Any idea how to fix this?

  • 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-05-22T02:20:35+00:00Added an answer on May 22, 2026 at 2:20 am

    There’s a bg trap in Powershell: , is the array operator. Try this at the command line:

    PS> 1,2,3
    

    You’ll see that it’s an array:

    PS> (1,2,3).gettype()
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    

    Try to call Add without commas:

    PS> Add 1 2 3
    Sum is 6
    

    Don’t forget everything is an object in Powershell. You are playing on top of .NET.

    So you have two friends:

    1. The gettype() method which gives you the type of an object
    2. The Get-Member CmdLet which helps you on properties and methods of an object

    Get-member has many parameters that can help.

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

Sidebar

Related Questions

Warning - I am very new to NHibernate. I know this question seems simple
I am really very new to powershell. I want to use powershell to read
Very new to this, and I have no idea where to start. I want
I am trying to integrate some new code I wrote for programmatically interacting with
im very new to this and have a very basic knowledge php and SQL
im very new at javascript so, this is the question. I need to make
I'm very new to COM and Windows programming/scripting in general. What I was trying
This seems like a very basic thing to do, but I am new to
Very new to jQuery, if dupe question sorry at this point not even sure
I am very new to Powershell script. I am trying to run a powershell

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.