0

Using Out-GridView, how do you control what the column names are? I would like something meaningful for the column names instead of “string” - is this possible?

[string] $colLabel = 'MyLabel'
[string] $a = 'a'
[string] $b = 'b'
$Selected = ($colLabel), ($a), ($b) | Out-GridView 
ornerygoat
  • 13
  • 5
  • Make it an array of **objects** as in `[PsCustomObject]@{'MyLabel' = 'a'}, [PsCustomObject]@{'MyLabel' = 'b'} | Out-GridView` – Theo Jul 24 '20 at 14:23
  • @Theo I think $ColLabel is the Column header and $a and $b are the properties. See my answer – Wasif Jul 24 '20 at 14:24
  • @Wasif_Hasan I didn't use `$ColLabel` or any of the variables. It is to show how it works, that's all. Have you tried it? – Theo Jul 24 '20 at 15:04
  • @Theo tried it, yours work. But I was just saying about $ColLabel as the column header, like CSV files/ – Wasif Jul 24 '20 at 16:15

2 Answers2

1

The typical pattern would be:

[pscustomobject]@{
  colLabel='MyLabel'
  a='a2'
  b='b2'
} | Out-GridView

or

get-process | select-object name,id,ws | Out-GridView

Here's 1 example of a calculated property:

1 | select-object @{ n='Num'; e={$_} }

Num
---
  1
js2010
  • 23,033
  • 6
  • 64
  • 66
  • Nice, also A good idea is to add `[ordered]` flag with the HashTable, so the hashtable properties stay in the exact order. Also you could show use of calculated properties to modify properties piped from a Cmdlet. – Wasif Jul 24 '20 at 16:17
0

Sure, use a temporary CSV file:

[string] $colLabel = 'MyLabel'
[string] $a = 'a'
[string] $b = 'b'
@"
$($colLabel)`,
$($a)`,
$($b)
"@ | Set-Content temp.csv
Import-Csv temp.csv | Out-GridView
Remove-Item temp.csv

First it creates a temporary csv file, then imports and passes to grid view and then deletes it.

Wasif
  • 14,755
  • 3
  • 14
  • 34