Monthly Archive

Categories

Group-Object change in PowerShell v6.1

There’s a subtle Group-Object change in PowerShell v6.1.

In PowerShell v5.1 if you do this:

$ts = 'ffrluoluntlvxutxbdvbktgyyvsvcrkxoyfotzkzogcwuwycmnhuedk'
$ts.ToCharArray() | group

 

You get this:

Count Name Group
----- ---- -----
    3    f {f, f, f}
    2    r {r, r}
    3    l {l, l, l}
    5    u {u, u, u, u...}
    4    o {o, o, o, o}
    2    n {n, n}
    4    t {t, t, t, t}
    4    v {v, v, v, v}
    3    x {x, x, x}
    2    b {b, b}
    2    d {d, d}
    4    k {k, k, k, k}
    2    g {g, g}
    4    y {y, y, y, y}
    1    s {s}
    3    c {c, c, c}
    2    z {z, z}
    2    w {w, w}
    1    m {m}
    1    h {h}
    1    e {e}

 

If you repeat the exercise in PowerShell v6.1 you get this:

Count Name Group
----- ---- -----
    2    b {b, b}
    3    c {c, c, c}
    2    d {d, d}
    1    e {e}
    3    f {f, f, f}
    2    g {g, g}
    1    h {h}
    4    k {k, k, k, k}
    3    l {l, l, l}
    1    m {m}
    2    n {n, n}
    4    o {o, o, o, o}
    2    r {r, r}
    1    s {s}
    4    t {t, t, t, t}
    5    u {u, u, u, u...}
    4    v {v, v, v, v}
    2    w {w, w}
    3    x {x, x, x}
    4    y {y, y, y, y}
    2    z {z, z}

 

In PowerShell v6.1 the results are automatically sorted by the Name (the same behaviour is exhibited in PowerShell v6.2 preview 3). That may, or may not, be what you want. Not sure exactly when, or even why, this behaviour changed.

 

When I’m using Group-Object I’m usually looking for the most or least common item so I tend to sort on value.

 

I don’t understand the value of this change and suspect its yet another change to PowerShell that was introduced to meet someone’s pet view of the world – one of the major problems of open source projects no one seems to really apply the “just because we can doesn’t mean we should filter”.

Comments are closed.