Out-Gridview removes underscores

540 Views Asked by At

Consider the following code:

$a = @()

$b = "" |select ho_ho,ha_ha
$b.ho_ho = "1"
$b.ha_ha = "2"
$a+=$b

$b = "" |select ho_ho,ha_ha
$b.ho_ho = "3"
$b.ha_ha = "4"
$a+=$b

$a | Format-Table -AutoSize
$a | Out-GridView

Using Format-Table, the underscores on the column headers are retained.

ho_ho ha_ha
----- -----
1     2
3     4

However, when using Out-Gridview, the underscores are automatically removed?

Underscores removed

Does anyone know how to avoid this?

1

There are 1 best solutions below

1
On BEST ANSWER

This seems to be related to the fact in WPF that the first underscore in a text prefixes the accelerator character for the control.

See this blog post for details:

WPF uses an underscore character instead of the ampersand character (like with WinForms) to prefix an access (a.k.a. accelerator or mnemonic) key in the text of its elements like Label and Button.

You can escape the underscore by using two underscores.

So this code would display Ok in the gridview but not in the Format-Table output

$a = @()

$b = "" |select ho__ho,ha__ha
$b.ho__ho = "1"
$b.ha__ha = "2"
$a+=$b

$b = "" |select ho__ho,ha__ha
$b.ho__ho = "3"
$b.ha__ha = "4"
$a+=$b

$a | Format-Table -AutoSize
$a | Out-GridView

Please note that the escaping is only necessary on the first underscore in the string, not the others.

I think this may be regarded as a bug (since it's not actually adding any key shortcuts either), but I couldn't find any reports on http://connect.microsoft.com