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")})
  • Thank you, this is perfect! I'll upvote as soon as I have 15 reputation. – cheeesus Apr 20 '12 at 11:23
  • 1
    Not sure you can "force" it. @(1) | ConvertTo-Json still returns 1 instead of [1]. – Marc Dec 6 '16 at 12:19
  • @Marc: ConvertTo-Json never returns a collection: it reads the whole input and converts to a single string. If you want input objects individually converted you'll need to process each separately. – Richard Dec 6 '16 at 13:05
  • 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
  • @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

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*
  • 5
    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

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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