1

I have a function that creates a hashtable and returns it. My problem is that when returned, its converted to a system object with 2 entried, and my hashtable at entry 3.

sample code:

Function Get-MyHashtable($props) {
    $NewProps = New-Object System.Collections.Hashtable
    $NewProps.add('Item1', $props.Value1)
    $NewProps.add('Item2', $props.Value2)
    return $Newprops
}

And my code, that uses the function

$NextgenProps = Get-Myhashtable -props $Somedatatoprocess

My challenge is that before returning $Newprops, it's a hashtable ($Newprops.gettype()) but $NextgenProps is a system.object containing 2 values ( 0 & 1) and then my hashtable.

I was expecting a hashtable returned, not a system.object.

3
  • 2
    I am unable to reproduce the issue (Get-MyHashtable -props @{ value1 = 'foo'; value2 = 'bar' }).GetType() is Hashtable. You need to give an example of what $Somedatatoprocess is. And double check if the function definition you have provided is exactly what you have. Commented Sep 20, 2023 at 15:03
  • 1
    What is $Props? An ArrayList? ... How did you create this? (are the entries really from your function?) Commented Sep 20, 2023 at 15:54
  • 1
    To add to iRon's comment: Note that any output - be it from a PowerShell command, an operator-based expression, or a .NET method call - that is neither captured in a variable nor redirected (sent to a file or via the pipeline to another command) is implicitly output from a script or function. To simply discard such output, use $null = .... If you don't discard such output, it becomes part of a script or function's "return value" (stream of output objects). See this answer for more information. Commented Sep 20, 2023 at 17:01

1 Answer 1

3

This happens because PowerShell functions don't actually have well defined return types, and because much of what happens inside of a function is put into the pipeline. When you return from this function, all of its output is placed into your variable.

If the code snippet you provided is actually the entire function definition then it is not clear where the extra values are coming from. For example, some methods such as the Add method of the system.collections.arraylist return the index of the item just added. If such a method were used in your function, that would explain why you are seeing the values 0 and 1 returned. However, the add method of the hashtable does not do this. Running your code as written I get a hashtable returned with 2 properties.

In the event that you do have something in your function which outputs info such as the index of the item just added then you would solve this problem with the following:

$myObject.MethodThatReturnsSomeValue() | out-null

Out-Null prevents those values from ending up in the pipline.

Sign up to request clarification or add additional context in comments.

1 Comment

See also the answer from this "Tracing where output is coming from in a Powershell script" question to figure out where it happend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.