Home > PowerShell Posts, Technology > PowerShell – Out-GridView Headers

PowerShell – Out-GridView Headers

I’ve liked the simplicty of PowerShell when using the Out-GridView against a data set when you want a quick sortable list.

If you use Out-GridView most likely you’re cutting and pasting into an Excel sheet

Out-GridView dosen’t allow you to copy the header row. But, a simple process will help

For example:

From example #4 on this page. https://technet.microsoft.com/en-us/library/hh849920.aspx

($A = Get-ChildItem -Path $pshome -Recurse) | Out-GridView

This displays the header bar but you’re unable to copy this header directly that I’ve not found.

outgridview1

CTRL+A and no grid headers selected.

outgridview2

Now create a simple object and function to run for storing header values:

$global:ReportHeaders = @()

function Report-Headers() {
 # There are a few methods for createing objects. Keeping this simple
 # to show the possibilities

# Sets up dummy object with headers for use in out-gridview
 $local:TestCollection = @()
 $TestProperties = new-Object Object
 add-Member -inputobject $TestProperties -membertype NoteProperty 
-Name "Mode" -Value ""
 add-Member -inputobject $TestProperties -membertype NoteProperty 
-Name "LastWriteTime" -Value ""
 add-Member -inputobject $TestProperties -membertype NoteProperty 
-Name "Length" -Value ""
 add-Member -InputObject $TestProperties -MemberType NoteProperty 
-Name "Name" -Value ""

$TestProperties.Mode = "Mode"
 $TestProperties.LastWriteTime = "Last Write Time"
 $TestProperties.Length = "Length"
 $TestProperties.Name = "Name"
 $local:TestCollection += $TestProperties
 $global:ReportHeaders = $local:TestCollection
 }

Report-Headers
 ($A = Get-ChildItem -Path $pshome -Recurse) | Out-GridView
 $global:ReportHeaders +=  $a
 $global:ReportHeaders | Out-GridView

Now there are simple headers to copy into excel or other tools.

outgridview3

Sample Excel:

outgridview4

Well, something very simple. If you’ve found a different way to do this please let me know.

Joseph

  1. March 10, 2017 at 12:00 pm

    Hi Joseph,

    I like your workaround!
    And I am a bit confused as how to put your suggestion into action.
    Could you elaborate a bit more how to get from your example to a working solution for any request to Out-GridView?

    Thanks a ton,
    Dennis

  2. March 10, 2017 at 12:02 pm

    Dear Joseph,

    I wanted to put your nice workaround into action but wasn’t able to do so.
    I tried to execute parts of your script but I can’t get it to work with arbitrary headers.

    Could you elaborate a bit more, what steps to follow to get those headers included?

    Thanks,
    Dennis

    • March 10, 2017 at 5:07 pm

      Hi Dennis,
      This solution will fit into most any scenario. What are you trying to do and I can show you how to put this into play for you.
      Thank you,
      Joseph

      • Dennis
        March 10, 2017 at 7:02 pm

        Hi Joseph,
        thanks for your quick response!
        I want to have the headers in the list when I am doing e.g.
        Get-Process | ogv
        I tried to define the array like you did before and tried to paste parts of your script into the shell, but to no avail.
        Can you give me a little bit more step by step instructions ;)?
        Regards,
        Dennis

      • March 13, 2017 at 10:03 pm

        Hi Dennis,
        Please look at a new post I’ve created with further explanations. PowerShell – Out-GridView Headers (Using Get-Process)
        on creating simple report headers.
        Thank you,
        Joseph

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: