Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

A Better PowerShell WPF Grid Viewer

Posted on February 22, 2019February 22, 2019

A while ago I added a command called ConvertTo-WPFGrid in my PSScriptTools module. I wanted something similar to Out-Gridview but done in a WPF form. The initial version was ok and got the job done but it always felt lacking. The new version of the command is greatly improved I think. I wanted to take a few minutes and show you a new shiny toy.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Auto Size

The original version of the command let you specify dimensions of the final form. The current version will auto size the form based on the size of the datagrid control.  For the most part I like this approach. Although the code I'm using may briefly display a blank form before everything is resized. And I no longer can have the form auto center on the screen. I have some issues open on these items in the Github repo and will eventually get around to addressing them.

Auto Refresh

The previous version had a timeout parameter which would automatically close the form after a user specified number of seconds. That feature remains. But I've taken it a step further. If you write a one line PowerShell expression that is piped to ConvertTo-WPFGrid, the function will parse out the command to build a scriptblock of all the code leading up to ConvertTo-WPFGrid. Now, if you use -Refresh and -Timeout, the data will be updated at that interval.

Get-VM | Select Name,State,CPUUsage,@{Name="MemAssignedMB";Expression = {$_.MemoryAssigned/1MB -as [int]}},@{Name="MemDemandMB";Expression={$_.MemoryDemand/1MB}},@{Name="Runtime";Expression= { "{0:dd\.hh\:mm\:ss}" -f $_.Uptime}} | ConvertTo-WPFGrid -Title "VM Status" -Refresh -Timeout 20

In order for this to work the command needs to be one long pipelined expression. It won't work with nested prompts or if you break the command at a | in the PowerShell ISE.

An auto refreshing grid

This offers up some terrific possibilities.

You could create a one-line script to build a small monitoring tool.

Get-Volume -CimSession (get-content c:\work\names.txt) | Where-Object {$_.DriveLetter -AND $_.DriveType -eq 'fixed'} | Select-object @{Name="Server";Expression={$_.PSComputername.toUpper()}},DriveLetter,HealthStatus,FileSystemLabel,@{Name="SizeGB";Expression={[int]($_.Size/1GB)}},@{Name="PctFree";Expression = {($_.sizeremaining/$_.size)*100}} | ConvertTo-WPFGrid -Title "DiskStatus" -Refresh -Timeout 60

Creating a disk monitoring tool with PowerShell and ConvertTo-WPFGrid

Background Runspace

This is possible because the new form runs in a separate runspace which means your PowerShell prompt is no longer blocked.  However this also means that your profile scripts aren't loaded so anything that you rely to get the display data won't be loaded. That's another issue I'll have to address. Related to that is that when you close the grid, the runspace remains. I have yet to find a technique to close the parent runspace when the form closes. If you display several forms you'll be left with runspaces.

Left over PowerShell runspaces

The first runspace is almost always your primary PowerShell session. They sessions will be removed when you close PowerShell. Or you can use the Remove-Runspace command also in the latest version of the PSScriptTools module.

Removing runspaces

Try for Yourself

You can try all of this for yourself. Install the PSScriptTools module from the PowerShell Gallery. Be sure to read help for the new commands. Convertto-WPFGrid converts the entire incoming object so be sure to use Select-Object to specify what you want to see in the form.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

6 thoughts on “A Better PowerShell WPF Grid Viewer”

  1. Matthew says:
    March 6, 2019 at 4:39 am

    Hello,
    I would like to suggest 2 enhancements.

    – import of user variables because you use scriptblock parameter
    $InitialSessionState = [InitialSessionState]::CreateDefault()
    if ($AddVariable) {
    $AddVariable | foreach-object {
    $var = $_.split(‘:’,2)
    $var = if (@($var).count -eq 1) {
    get-variable $_ -ErrorAction 0
    } else {
    get-variable $var[1] -Scope $var[0] -ErrorAction 0
    }
    if ($var) {
    $varEntry = New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry(
    $var.name, $var.value, $null)
    $InitialSessionState.Variables.Add($varEntry)
    }
    }
    }
    $newRunspace = [RunspaceFactory]::CreateRunspace($Host, $InitialSessionState)

    – grid scalability.
    When I resize grid window the grid is not resized. If my table very large, window takes all the screen width and it is impossible to resized it without moving it. I believe it is not good idea to take all the screen width.

    1. Jeffery Hicks says:
      March 6, 2019 at 8:03 am

      Did you test this using v2.3.0 of the PSScriptTools module? I made some changes that affect sizing and resizing that might address some of your issues. I’ll have to look at the variable issue.

      1. Matthew says:
        March 6, 2019 at 8:25 am

        I have used it.
        As I resize window vertically the grid is not resized.
        One more Q. How to disable in cell editing?

      2. Jeffery Hicks says:
        March 6, 2019 at 8:32 am

        I’ll add these to the list.

      3. Jeffery Hicks says:
        March 6, 2019 at 3:00 pm

        Test out the revised function in v2.4.0 of the PSScriptTools module.

  2. Donald M says:
    March 12, 2019 at 9:28 pm

    This solves a big problem, awesome module! I may try to integrate this into the AnyBox; very cool!

    https://github.com/dm3ll3n/AnyBox

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d