48
$result = Get-ADUser -Filter $filter

If I have 2 or more results, I get $x as array, but if I have only one result, a get $x as object. How to make it more correct, to always recieve array - empty, with one element or with some elements?

filimonic
  • 3,988
  • 2
  • 19
  • 26
  • 5
    I'll drop the downvote this time, but you should search before asking. This is answered multiple times and is PowerShell basics = no research effort. – Frode F. Dec 29 '12 at 20:38
  • 1
    possible duplicate of [How can I force Powershell to return an array when a call only returns one object?](http://stackoverflow.com/questions/11107428/how-can-i-force-powershell-to-return-an-array-when-a-call-only-returns-one-objec) – Alex Angas Nov 07 '14 at 04:24

4 Answers4

57

Try $x = @(get-aduser)

The @() syntax forces the result to be an array

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
  • 1
    This won't result in an array containing an array if the result is an array? What if the function results `$null`? (PowerShell is so confusing because of all the magic they do sometimes.) – jpmc26 Mar 11 '15 at 17:01
  • This is one of those annoying things about Powershell. With this solution you'll have an array with one element which is an array, so your Count property will be 1 even though the array inside could have many elements, which means you can't use it directly in a for loop. – David Klempfner Feb 08 '17 at 06:15
  • @DavidKlempfner, in most cases you will not have array with single element. – filimonic Aug 07 '20 at 09:48
  • Years later and I came across this problem again. This time a for loop was not entering because the array was turned into an object because it was one element. I had to check the Count property for null before entering the for loop. – David Klempfner Jan 25 '21 at 06:21
17

By the way, the other solutions in this question are not really the best way to do this, for the reasons stated in their comments. A better way is simply to put a comma before the function, like

$result = ,(Get-ADUser -Filter $filter)

That will put an empty result into an empty array, a 1 element result into a 1 element array and a 2+ element result into an array of equal elements.

massospondylus
  • 179
  • 1
  • 2
4

Also, you can use $x=[array]get-aduser

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
0

I had the same issue using an indexed value in a loop. I fixed it by changing

$PatchGroupData.SCCM_Collection[$i]

to

@($PatchGroupData.SCCM_Collection)[$i]
Micer
  • 8,731
  • 3
  • 79
  • 73
  • This is effectively the same answer as the currently accepted one, but with less information. Is there more you can add to your answer to flesh it out? – Ross Mar 29 '18 at 18:06
  • I will add comment. Some collections are internally `IEnumerable` type. IEnumerable does not have index (`[n]`) accessor, so they should be converted to array to have index. Theoretically, IEnumerable sequence CAN be infinite, so conversion to finite array not possible, but this is very rare case. – filimonic Aug 07 '20 at 09:46