Search

sort-object kill array with 1 element by brima

Active

1
3
Sign in
to vote
Type: Bug
ID: 774542
Opened: 12/17/2012 5:17:22 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
if you hav a array with only 1 element and sort this with sort-object it destroy the array.
I have a script that list the groups (member of) for AD-User and sort this groups. If the user is only in one group this not work, after the sort-object the array is not longer a array.

PS U:\> $GroupTable = @()
PS U:\> $GroupTable += "group1"
PS U:\> $GroupTable[0]
group1
PS U:\> $GroupTable -is [Array]
True
PS U:\> $GroupTable = $GroupTable | Sort-Object
PS U:\> $GroupTable[0]
g
PS U:\> $GroupTable -is [Array]
False

My workaround for the problem:

PS U:\> $GroupTable = @()
PS U:\> $GroupTable += "group1"
PS U:\> $GroupTable[0]
group1
PS U:\> $GroupTable -is [Array]
True
PS U:\> [array]$GroupTable = $GroupTable | Sort-Object
PS U:\> $GroupTable[0]
group1
PS U:\> $GroupTable -is [Array]
True
Details (expand)
How often does this happen?
Sometimes Happens

Have you seen this problem before in this product?

Yes, this happens in all previous versions
Reproduction Steps
$GroupTable = @()
$GroupTable += "ABCDEF"
$GroupTable[0]
$GroupTable -is [Array]
$GroupTable = $GroupTable | Sort-Object
$GroupTable[0]
$GroupTable -is [Array]
Expected Results
Sort-Object should not destroy the array, if the cound from the array is < 2.
File Attachments
0 attachments
Sign in to post a comment.
Posted by PetSerAl on 1/15/2013 at 8:44 PM
It not Sort-Object fault, it is how PowerShell pipeline works.
Sign in to post a workaround.