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?


6 Answers 6


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")})
  • 3
    Not sure you can "force" it. @(1) | ConvertTo-Json still returns 1 instead of [1].
    – Marc
    Dec 6, 2016 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, 2016 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, 2016 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, 2016 at 8:12
  • 4
    ConvertTo-Json -InputObject @(1) outputs [1]. You need to explicitly wrap arrays into @(...) Mar 30, 2017 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.

  • 1
    We just ran into this - and this was the only answer that helped us.
    – sylvanaar
    Jan 15, 2020 at 15:15
  • You sir are a god in my eyes, kudos to you
    – Dr Schizo
    Jun 14, 2021 at 13:41

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

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

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*
  • 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, 2013 at 12:15
  • 1
    The behaviour is still the same in PS 5.
    – MEMark
    May 4, 2016 at 7:46
  • Yep, def still present Jul 14, 2017 at 10:16
  • 1
    This behaviour is still the same in PS 6.0.1
    – spuder
    Apr 5, 2018 at 23:21

This worked for me, but I had to declare the results in a new variable, or PowerShell just kept breaking down the array into a single object...most frustrating.

Thank you Microsoft for not considering the use case of JSON...insert seething sarcasm

$result = ConvertTo-Json -InputObject @($body) -compress
  • Doesn't help when your body object has child properties which are also arrays ... API's that accept JSON as input do not natively assume a string as same schema as an array of one object :(
    – felickz
    Jul 1, 2021 at 21:31

In your case, you should use count rather then length.

> (ls | %{$_.Name} | ?{$_.Contains("og")}).count
> (ls | %{$_.Name} | ?{$_.Contains("Prog")}).count

count returns

  • number of element, for array like objects,
  • 1, for scalar objects,
  • 0, for $null.

Note: count does not work for pscustomobject in versions older than PowerShell 6.1, which was a bug: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.1

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.