Home > OS >  Powershell | Array - Select from - to
Powershell | Array - Select from - to

Time:12-07

is there a way to select some objects of an array "from-to"?

$data = @('A1','A2','A3','A4','B1','B2','B3','B4')
$data |Select-Object A3 - B2

in my example i want to select only A3-B2 but i dont want to write them down like this

 $data |Select-Object A3, A4, B1, B2

CodePudding user response:

Since you are operating on array, you can directly slice the array from 2 to 5. ie,

$data[2..5]

If you still want to use Select-Object, you can use it with -Index parameter. This will Selects objects from an array based on their index values.

$data | Select-Object -Index @(2..5)

CodePudding user response:

You can retrieve part of the array using a range operator for the index in your case it will be from the 3rd item to the 6th

$data [2..5]

or you can do this with negative numbers counts

$data [-3..-6] you can read more on the documentation

  • Related