Count property invalid if only one instance

Welcome Forums General PowerShell Q&A Count property invalid if only one instance

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5864

    Points: 0
    Rank: Member

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

    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.

    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.


    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.


    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
    PS F:\Storage\Scripts\Windows\ScriptingGames> $psversiontable

    Name Value
    ---- -----
    WSManStackVersion 3.0
    PSCompatibleVersions {1.0, 2.0, 3.0}
    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...

The topic ‘Count property invalid if only one instance’ is closed to new replies.