I have created a List<string> in PowerShell and want to sort this list using a script block with the code below:
$AssemblyNames = New-Object "System.Collections.Generic.List[string]"
$AssemblyNames.Add("PersonService.BizTalk.dll")
$AssemblyNames.Add("PersonService.BizTalk.Schemas.dll")
$SortedAssemblyNames = $AssemblyNames | Sort-Object { if ($_.ToLower().Contains("schema")) { 0 } else { 1 } }
$SortedAssemblyNames
which outputs:
PersonService.BizTalk.Schemas.dll
PersonService.BizTalk.dll
All fine, but I want to remove some elements later in processing these elements using:
$SortedAssemblyNames.Remove($SortedAssemblyNames.Item($i))
This produces the error
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size.
So I checked the type of the $SortedAssemblyNames variable and I get System.Object[]. And this is strange as the Sort-Object documentation states:
Outputs
The output type is the type of the objects that the cmdlet emits.
And in my opinion, this should be System.String[]. Am I wrong or is there a way to specify the output type of the Sort-Object cmdlet?
My current workaround is to loop through this collection and add it to a new List<string> but I think there should be a better solution as I need to modify the collection later and arrays are a pain.