3

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42

1 Answers1

2

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

Tom V
  • 1,498
  • 18
  • 24
  • Excellent find (nice feature-bug :) ). Actually it does add key shortcuts if you use a different letter after the 1st '_'. E.g.: `(1,'c'),(2,'b'),(3,'a') | Select-Object -Property @{Name='ab_cd'; Expression={$_[0]}}, @{Name='ef_gh_ij'; Expression={$_[1]}} | Out-GridView`. When you press Alt, 'c' & 'g' will become highlighted. Pressing Alt+C or Alt+G will sort by that column (alternating between Ascending & Descending). – Petru Zaharia Oct 23 '18 at 00:50