1

I am trying to let Get-ChildItem retrieve the contents of a folder, and show the size of each object in the folder, the name and the type of object (file/directory). I am however having trouble getting only those three columns to work.

The name and length are rather straightforward, but how do I display differences between files and folders?

2 Answers 2

6

Check the PSIsContainer property. It is set to $true for folders and $false for files. Here's an example:

Get-ChildItem | Foreach-Object{ New-Object PSObject -Property @{ Name = $_.Name SizeMB = if($_.PSIsContainer) { (Get-ChildItem $_.FullName -Recurse | Measure-Object Length -Sum).Sum/1mb } else {$_.Length} Type = if($_.PSIsContainer) {'Directory'} else {'File'} } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Once again the winning answer. So if I am correct, you create a property object for each object in the folder. And for each property object you assign a name value, a sizevalue and a type value based on the previous value. The only thing I dont understand is how you defined the size. I think you use the -recurse switch to measure the whole directory's size, and for files you use the Length? Does Measure-Object not work for files aswell? Either way, thanks again!
A file has a Length property so we don't need anything else but it to know the size. On the other hand folders do not have a Length property. To calculate the folder size we need to iterate over all of its files (recursively) ans sum up the length of all of them. That's where the Measure-Object cmdlet comes handy.
Nice algorithm, @Shay! However, there is one inconsistency--you are calculating megabytes for directories but just bytes for files. The simplest fix is to change the property name from SizeMB to Size and delete the divide-by-1mb on the folder size calculation.
You're absolutely right @msorence, I added MB calculation just to show that it's possible. I'll leave it up to reader to decide if he wants the result in bytes or MB :)
3

I believe that this is possible by taking the 'Mode' information. ( See this answer here )

Basically, then, anything listed by the Get-ChildItem command whereby the 'Mode' begins with "d".

Here's the actual meanings of the 6 possible values in 'Mode', if you didn't want to go to @Joey's answer:

In any case, the full list is:

d - Directory a - Archive r - Read-only h - Hidden s - System l - Reparse point, symlink, etc. 

So I've used this to output a CSV, which I'll then flag the folders in excel, or my Flow, or SharePoint. The command I've used is:

Get-ChildItem -Recurse | Select-Object DirectoryName, BaseName, Extension, Mode | Export-Csv -Path INSERTPATH\listings.csv -Encoding ascii -NoTypeInformation 

Hope that this helps anyone that comes across it.


Caveat, this is first time that I'm answering with the full intent of answering. I believe that even though this is 6 years old, people will still find it useful when trying to achieve the same thing.

Comments