Search code examples
powershelldirectorychildren

PowerShell (Get-ChildItem).Length bug


I'm learning to use Windows PowerShell and I noticed that (Get-ChildItem).Length returns file size when there is only one file in the directory. Do I have to write my own function to get the length or is there a simpler method?


Solution

  • That's not a bug. When there is more than one file, the command will return an array. With the property Length you will get in this case the size of the array. Some example how to get the size:

    Get-ChildItem | select -ExpandProperty Length
    

    or

    If you want to manipulate the values further, try maybe something like that

    Get-ChildItem | foreach {$_.Length}