Author Posts

January 1, 2012 at 12:00 am

by Lembasts at 2013-04-29 17:37:15

Greetings,
Recently I was doing a get-childitem and accessing the count property of the returned collection.
What I found was that this property was effectively null if only one instance was returned in the collection.
I found an article on the web that said it was a "known feature" and that it could be fixed by forcing the array e.g. using @($coll).count instead of $coll.count.
http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2012/03/19/Counting-objects-in-PowerShell-3.0.aspx

This worked fine. However, the article went on to say that this "feature" was "fixed" in V3 of Powershell and that the count property would indeed return a value of one if there was only one instance.
I ran my code on two different machines, both with POSH V3 and it would not return a value of one.

Explanations?

by happysysadm at 2013-04-29 18:12:07

Hi David,

on v3 it works well for me: (gci).count with only one file returns 1, while on v2 it returned nothing.

Check $psversiontable and make sure your are effectively using v3.

Carlo

by Lembasts at 2013-04-29 18:33:52

I have checked $psversiontable and I am definitely using V3.

by MasterOfTheHat at 2013-05-01 08:29:19

Very strange... works like it's supposed to on mine:
PS F:\Storage\Scripts\Windows\ScriptingGames> gci

Directory: F:\Storage\Scripts\Windows\ScriptingGames

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/30/2013 8:47 AM 417 Beginner1.ps1

PS F:\Storage\Scripts\Windows\ScriptingGames> (gci).Count
1
PS F:\Storage\Scripts\Windows\ScriptingGames> $psversiontable

Name Value
---- -----
WSManStackVersion 3.0
PSCompatibleVersions {1.0, 2.0, 3.0}
SerializationVersion 1.1.0.1
BuildVersion 6.2.9200.16481
PSVersion 3.0
CLRVersion 4.0.30319.296
PSRemotingProtocolVersion 2.2

I assume your box is updated to the latest build versions, etc? I'm not sure what patches have been released for v3...