PowerShell Out-GridView

1.7k views Asked by At

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 
2

There are 2 answers

1
js2010 On

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
0
Wasif On

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.