PowerShell – Out-GridView Headers

I’ve liked the simplicity of PowerShell when using the Out-GridView against a data set when you want a quick sort-able 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.

Out-GridView No Headers
Out-GridView No Headers


CTRL+A and no grid headers selected.

Out-GridView No Headers Selection
Out-GridView No Headers Selection

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.

Out-GridView with Headers

Out-GridView With Headers
Out-GridView With Headers

Excel Sample

Out-GridView Copy Results To Excel
Out-GridView Copy Results To Excel

Simple PowerShell

Well, something very simple. If you’ve found a different way to do this please let me know. In a pinch PowerShell can create some simple report headers that are good for automating simple excel reports.

Thoughts & Ideas, Joseph Kravis 🙂



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

  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

    • 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

      • 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

      • 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

Leave a Reply

%d bloggers like this: