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

vimal mishra
  • 1,087
  • 2
  • 12
  • 32

1 Answers1

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(...)

filimonic
  • 3,988
  • 2
  • 19
  • 26