Home > OS >  Powershell - question re totalling up file lengths
Powershell - question re totalling up file lengths

Time:07-13

Powershell beginner here.

While having a look at this question: Powershell - Search directories - Find specific extension and total files for each directory I came across some PS on another website that goes a little part of the way towards answering the question posed. A little part of this Powershell is below:

Write-Host "Total Size of *.$Extension files: $('{0:N2}' -f (($Files | Measure-Object length -Sum).Sum / 1mb))MB`n`n"

In running the PS from the other webpage (in total. ie not just the quoted line above) I get the desired results indicating a total size for all extensions queried. That's fine, but I need help understanding the line quoted above. I can see that this is what is doing the adding up of file sizes, but...

  1. What does ('{0:N2}' this mean?
  2. What function does the second "sum" (.Sum) have?

Thanks in advance.

CodePudding user response:

What does ('{0:N2}' this mean?

It's normal PS formatting, see details per this link.

https://devblogs.microsoft.com/scripting/powertip-formatting-numeric-output-using-powershell

https://devblogs.microsoft.com/scripting/use-powershell-and-conditional-formatting-to-format-numbers

What function does the second "sum" (.Sum) have?

The final value to be returned. It's called using dot parameter method and is used to get a specific property value.

Simple example:

(Get-ChildItem).FullName

See also:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_properties?view=powershell-7.2

So, not unique to what you've posted. These is all normal basic/intermediate PS function call with formatted output.

All of this is shown by example in the PS help files on your system and on the MS docs site.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-7.2

CodePudding user response:

@postanote has answered this well. I'm happy. Cheers :-)

  • Related