Add Custom Output to Windows PowerShell GridView

Use custom output in GridView with this tip.

John Savill

August 3, 2013

1 Min Read
Add Custom Output to Windows PowerShell GridView

Q: I have a custom set of data I am creating in Windows PowerShell that I want to use with normal PowerShell formatting, such as format-table and Out-GridView--how can I do this?

A: I recently created a PowerShell script to list and execute System Center 2012 Orchestrator runbooks. However, when fetching the runbooks, I had to parse through an array and extract certain elements, for example:

foreach ($runbook in $output.feed.entry){    Write-Output "Name:" $runbook.content.properties.Name    Write-Output "ID:" $runbook.content.properties.ID.InnerText    Write-Output "IsMonitor:" $runbook.content.properties.IsMonitor.InnerText}

The problem is this can't be piped to other formatting type commands or anything else. The solution is to instead create a hash table which can consist of a structured array of named values, and then this hash table can then be piped and used as a regular set of PowerShell objects.

First an empty hash table is created:

$OutData =@() # Build structure for the results

Then for each object that should be added (with each object consisting of a number of attributes) a generic PSObject is created with members added with a Name and Value. For example:

# Output properties of each job in page to host        foreach ($runbook in $output.feed.entry)        {          $RunbookDetail = New-Object PSObject          $RunbookDetail | Add-Member -Name "Name" -MemberType NoteProperty -Value $runbook.content.properties.Name          $RunbookDetail | Add-Member -Name "ID" -MemberType NoteProperty -Value $runbook.content.properties.ID.InnerText          $RunbookDetail | Add-Member -Name "IsMonitor" -MemberType NoteProperty -Value $runbook.content.properties.IsMonitor.InnerText          $OutData += $RunbookDetail        }

The content of OutData can be piped to any standard PowerShell cmdlet including Format-Table, Out-GridView and more. For example:

The output is shown below.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like