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



Categories: PowerShell Posts, Technology

Tags: , , ,

5 replies

  1. 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

    Like

  2. 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

    Like

    • 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

      Like

      • 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

        Like

      • 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

        Like

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

Kraviparts

We'll find the tough parts.

Pretty Things, Nice Life!

Secondlife Beauty

The Dionysian Experience

Sensual, Thoughtful, and Very Naughty

The Voice of Peace

Be the peace you are looking for

slovesadventures.wordpress.com/

SLovesadventures: The Grid... Through Love Trill's Eyes

Life via Window

Canvas of stories, fiction and feelings.

Yellingrosa's Weblog

Poetry, Visual Arts, Music and IT Tech

Cook up a storm

Cook Up a Storm

Behind the Camera

Second Life Style

%d bloggers like this: