How would I use powershell to count the number of files and the number of folders under a folder, emulating the Windows "properties" of that folder?

(get-childitem -recurse).count

returns the total object count (sum of folder and file counts), and

(gci -rec | where { $_.PSIsContainer }).count

returns the number of folders. But is there a way to count both folders and files in a single command without traversing the filesystem twice? I assume it's possible to write procedural powershell code with named variables and loops, but that doesn't seem like the normal way of using it.

And on a related topic, http://technet.microsoft.com/en-us/library/ee692796.aspx describes two ways of getting a list of filenames:

Get-ChildItem C:\Scripts | Select-Object Name


Get-ChildItem C:\Scripts -name

The author claims they are equivalent, but with -recurse the first returns only filenames while the second returns the full path. Is there a way to get Select-Object to return a full path so that these two commands generate the same output? (A random stab at Select-Object Path produced blank lines, not paths.)

gci -rec -name
gci -rec | Select-Object xxx?

closed as off topic by nhinkle Jun 11 '13 at 3:48

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Answer provided by by Shay Levy at stackoverflow.com/questions/17032252/…. – Dave Jun 10 '13 at 21:03
  • 2
    I see where your misunderstanding stems from, but this is not an appropriate use of cross-posting. The meta question you reference is old, and not an official statement. The official FAQ on cross-posting clearly states that cross-posting is not allowed. If you decide your question is a better fit elsewhere, please ask for it to be migrated, or delete and re-ask. The point of the meta post you referenced is that it's not inherently bad to have similar (but separate) questions on disparate sites, provided each is tailored to that site's audience. – nhinkle Jun 11 '13 at 3:52

You can grab the type of each item as you iterate through the files and then use the group-object commandlet to get a count of each type:

dir -rec | %{$_.GetType()} | group-object name | select name,count

Name          Count
----          -----
DirectoryInfo    15
FileInfo         26

To answer your second question, just select the FullName property:

Get-ChildItem C:\Scripts | Select-Object FullName

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