Consider the following Powershell script, which searches for folders in C:\ with a 'og' in their name:

PS C:\> (ls | %{$_.Name} | ?{$_.Contains("og")})
Program Files

Now I narrow down the search to get only one item:

PS C:\> (ls | %{$_.Name} | ?{$_.Contains("Prog")})
Program Files

The strange thing is that the first operation yields an array, whereas the second operation (which is IMHO semantically the same operation, so it should yield the same type of result) yields a string. This can be seen in the following result:

PS C:\> (ls | %{$_.Name} | ?{$_.Contains("og")}).Length
PS C:\> (ls | %{$_.Name} | ?{$_.Contains("Prog")}).Length

This can be very irritating, since apparently there are less folders which match 'og' than those who match 'Prog'.

Evidently, PowerShell implicitly 'unboxes' a single-item array to a single object, and we never get an array of length 1. It seems that every time I want to count the results coming over the pipeline, I have to check if I'm dealing with an array or not.

How can I prevent this from happening? How do you deal with this?


Evidently, PowerShell implicitly 'unboxes' a single-item array to a single object,

And zero item results to $null.

How can I prevent this from happening?

You can't.

How do you deal with this?

Use the array constructor (@(...)) to force a collection (possibly with zero or one elements) return:

$res = @(ls | %{$_.Name} | ?{$_.Contains("Prog")})
| improve this answer | |
  • Thank you, this is perfect! I'll upvote as soon as I have 15 reputation. – cheesus Apr 20 '12 at 11:23
  • 3
    Not sure you can "force" it. @(1) | ConvertTo-Json still returns 1 instead of [1]. – Marc Dec 6 '16 at 12:19
  • 1
    @Richard, I think you misunderstand: I, and many others, basically want the entire object (i.e. collection) serialized (e.g. for external persistance). We are not interested in processing each object in the collection separately. ConvertTo-Json should return a string which, if run through, ConvertFrom-Json returns the original object albeit an empty array/collection. – Marc Dec 7 '16 at 8:07
  • 1
    @Marc The point of this question is to avoid the treatment of a single element array as that element (which is less of an issue due to subsequent PSH changes: note the question's date). You are talking about a completely different case (forcing a collection to be a single object) hence me misunderstanding. – Richard Dec 7 '16 at 8:12
  • 1
    ConvertTo-Json -InputObject @(1) outputs [1]. You need to explicitly wrap arrays into @(...) – SliverNinja - MSFT Mar 30 '17 at 15:12

Note the difference between these two results:

PS C:\> ConvertTo-Json -InputObject @(1)
PS C:\> @(1)|ConvertTo-Json
PS C:\>

The point is that the 'unboxing' is being done by the pipe operation. ConvertTo-Json still sees the object as an array if we use InputObject rather than piping.

| improve this answer | |
  • 1
    We just ran into this - and this was the only answer that helped us. – sylvanaar Jan 15 at 15:15

This has been resolved in PowerShell v3:


On a side note, you can find if a name contains something using a wildcard:

PS> ls *og*
| improve this answer | |
  • 6
    Shay, I cannot comment on answers yet, but your statement is not true. PowerShell still boxes elements, but they have, as you noted, given single items a "Count" value. Single item results are still unboxed, though. You can test the above example against PS 3 and see the results. – Tohuw Jul 17 '13 at 12:15
  • 1
    The behaviour is still the same in PS 5. – MEMark May 4 '16 at 7:46
  • Yep, def still present – James Wiseman Jul 14 '17 at 10:16
  • 1
    This behaviour is still the same in PS 6.0.1 – spuder Apr 5 '18 at 23:21

An alternative to this problem is to explicitly set the variable type to be an array like this:

[array]$res = (ls | %{$_.Name} | ?{$_.Contains("og")})
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.