1
Using-
Windows 7 Enterprise SP1 64bit
PSVersion 5.0.10586.117
Build 10.0.10586.117

I am attempting to find the exact sum, maximum, minimum and average sizes (in bytes) of all of the files on the desktop of my user account. I am running the following command -

Get-ChildItem C:\Users\user\Desktop -recurse –File | Measure-Object -property length –sum -Maximum -Minimum –Average | Out-GridView -Title "Max, Min and Avg Size of Each File" 

The output comes out as-

count: 28
Average: 7,120.11
Sum: 199,363.00
Maximum: 87,040.00
Minimum: 0.00
Property: Length

However, the properties for my C:\Users\user\Desktop shows the following-

Size: 194 KB (199,645 bytes)
Size on Disk: 284 KB (290,816 bytes)

The Largest file on my desktop is 85.0 KB making the Maximum value result from the code wrong as well.

These numbers clearly do not match. What are the results that i'm getting from the PS command showing? They can't be the actual file sizes. The intended outcome is for the sum to match either the size or size on disk from the properties of my desktop. Please help me understand what my output is and how to get what I need.

Thanks!

5
  • Repeat your test on a few test folders you create yourself with known sizes including a mixture of hidden, system, read-only, and a baseline of "normal" files. Commented Jun 22, 2016 at 21:15
  • And now I have an answer as to why gci has a -Force parameter. The difference 199,645 - 199,363 = 282 bytes, which is exactly the size of the hidden desktop.ini file on my desktop. Using get-childitem -force makes the hidden files show up in the results as well, that will probably fix one of your questions. About the biggest file - |sort -property length | select -last 1 and see if PS is getting the same file you expect. Exactly what does Explorer show as the full filesize for it? Commented Jun 22, 2016 at 21:21
  • It appears as though all I needed to do was throw a -force like you suggested in directly after my -File flag before the first |. The output is now showing correctly. The file size for the sum matches in both the properties and the output. Same for the largest file which was reading exactly 87,040 bytes. Commented Jun 22, 2016 at 21:25
  • I'm going to test this a couple of more times in different areas doing what you guys suggested. I'll answer as soon as I can verify that I can recreate it. Thanks for the help so far. Commented Jun 22, 2016 at 21:32
  • It looks like it's working for Bytes. I did however, find that in the properties for one of the files I was checking that it shows the size as 116MB (122,187,392 bytes) I'm not sure how that's possible because that comes out to 122.187392 MB when you convert it. Aside from that it appears that the command is working properly when read in strictly bytes. Commented Jun 22, 2016 at 21:39

1 Answer 1

1

After testing Kory and TessellatingHeckler's ideas, I have found the fix. GCI requires the -force flag in order to see hidden files when calculating size. My working command is:

Get-ChildItem C:\Users\user\Downloads -recurse –File -Force |
    Measure-Object -property length –sum -Maximum -Minimum –Average |
    Out-GridView -Title "Max, Min and Avg Size of Each File"

This only works for calculations done in bytes. Keep that in mind.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget, PowerShell has byte converters build in as well. 87040 / 1kb returns 85.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.