Array Has Wrong Length After Sorting

The following script places a single file object into an array then sorts it. Before sorting, the array has length 1. After sorting, it has length 4117. What am I doing wrong?

$file = Get-ChildItem -File | Where-Object {$_.Name -eq "cert.pem"}
$nameList = @($file)
"namelist.length before sort: " + $nameList.Length
"NameList: " + $nameList
$nameList = $nameList | Sort-Object -CaseSensitive -Descending
"namelist.length after sort: " + $nameList.Length
"NameList: " + $nameList
namelist.length before sort: 1
NameList: cert.pem
namelist.length after sort: 4117
NameList: cert.pem

Lets go through it one step at a time.

#great simple

$file = Get-ChildItem -File | Where-Object {$_.Name -eq "test.txt"}

#ok namelist contains 1 file

$nameList = @($file)

#namelist (the array).length (how many objects in namelist? 1.

"namelist.length before sort: " + $nameList.Length

#writing that out

"NameList: " + $nameList

#namelist (1) = (1) sort object… ok still one item…

$nameList = $nameList | Sort-Object -CaseSensitive -Descending

#string CONCATENATION the length (1 file) of namelist. in you case your one file has a length (size) of 4117KB

"namelist.length after sort: " + $nameList.length

#ok

"NameList: " + $nameList

Are you wanting to find the length (size) of any of these files? if not try the Count property. you would of noticed if you had more than 1 item length would of given you the same result as count simply because you specifically asking for the length property of a single item (its size). sorry 6am here hope this makes sense!

Thanks for your help. That snippet was pared down from a larger script in which I was trying to construct an array of file objects, which I then sorted for later manipulation. So the Length I was looking for was not the size of a file, but the length (number of elements) of the array.

I think I’ve found the problem though. I was assuming that when I piped the variable representing an array object into Sort-Object, and assigned the result back to the original variable, then I would have a sorted array. That assumption seems to be false if the array only contains one item; in that case the object returned is the file object itself, rather than an array containing the file object.

I fixed it by changing:

$nameList = $nameList | Sort-Object -CaseSensitive -Descending

to:

$nameList = @($nameList | Sort-Object -CaseSensitive -Descending)

Powershell was unwrapping the array you were trying to make. So instead of iterating through a one item array, you iterated through the string object, which was the first line. If you output the the results of the sort you should see that it sorted the first line.

$nameList = @($file)

To force the creation of an array, without it immediately unwrapping, use this construct instead:

$nameList = , $file

By the way, the count and length properties are the same. The one is an alias for the other.

use $array.Count, instead of Length, it’s more ‘native’ for arrays.
Thus you avoid errors with alasing Count=Length for arrays and Size=Length for files :slight_smile: