sort-object kill array with 1 element - by brima

Status : 

 


1
3
Sign in
to vote
ID 774542 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 12/17/2012 5:17:22 AM
Access Restriction Public

Description

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
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.