Converting the Windows Script Host Count Method

Definition: Returns the number of members in an object.


In Windows Script Host the Count method is used to return the number of items in a WSH collection (such as the number of arguments in the Arguments collection, the number of folders in the Special Folders collection, etc.). In Windows PowerShell you can use the Length property to return the number of items in pretty much any collection. For example, suppose you want to know the number of command-line arguments that were supplied when a script was started. Well, by default PowerShell stores each command-line argument in the special variable $args. Thus you can determine the number of arguments in that collection (and thus the number of arguments supplied when starting the script) by checking the value of the length property of $args:


And, like we said, the Length property works for pretty much any collection. Want to know how many lines are in a text file? This should do the trick:

(Get-Content C:\Scripts\Test.txt).Length

Meanwhile, this command tells you the number of processes running on a computer:


Pretty cool, huh?

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page