mjsr
mjsr

Reputation: 7590

why select -index array doesn't work, powershell

i need to add numbers that don't belong to a linear sequence to retrieve specified indexed folders, i make this attempts :

$a =ls | ?{$_.psiscontainer} | sort creationtime

Then i try to select only some of the folders(fail):

$a | select -Index (100, 101, (103..109))

Whit this simplified array i don't have problems:

$a | select -Index (103..109)

how can i add those numbers?

I try this, forcing the conversion to an array, but the process of conversion fails and i don't know why, if i get the type of the inner members of the array they are already Int32, so i don't understand the error.

$a | select -Index @(100, 101, (103..109))

Upvotes: 3

Views: 3055

Answers (2)

mjolinor
mjolinor

Reputation: 68243

For this example, you can get the same result using array slicing:

$a[100,101 + 103..109]

Upvotes: 2

stej
stej

Reputation: 29449

Parameter -Index is of type int[]. That means that what you pass in as an argument, it has to be an array of numbers.

Imho the simplest way is just to add the arrays like this:

0..100 | select -Index (2,3,5 + 20..30 + 50,60)

Note that you don't have to do ((2,3,5) + (20..30) + (50,60)), because the comma operator has higher priority than plus and interval operator.

Upvotes: 7

Related Questions