3

I am returning the array in Powershell. But in response, I am getting array values along with the indexes of the respective values.

How I can stop the item index from printing in the console.

Code Example:

$data = [System.Collections.ArrayList]@()
$Item1 = "vimal" +"::"+ "error1"      
$data.add($Item1)
$Item1 = "vimal" +"::"+ "error2"      
$data.add($Item1)

return $data

Response:

0 1 vimal::error1 vimal::error2

I don't want to print 0 and 1 in the console.

Thanks, Vimal

1 Answer 1

3

ArrayList.Add returns index of an newly-added object. Those returns (all un-catched returns) are joined together

$data = [System.Collections.ArrayList]@()
$Item1 = "vimal" +"::"+ "error1"      
$data.add($Item1)   # <<== This outputs "0" because Add() returns index (0) of added object
$Item1 = "vimal" +"::"+ "error2"      
$data.add($Item1)   # <<== This outputs "1"

return $data  # <<== This outputs "..error1" and "..error2"

To suppress unwanted returns, use [void]$data.add(...) or $data.add(...) | Out-Null or assign value to some var: $indexOfItem1 = $data.add(...)

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

Comments

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.