As many of you know, this year I’ve made the jump to PowerShell Core as my “daily driver”. A major driving factor was to discover the limitations. There are certainly plenty of advantages as we move to a cross-platform world. But the PowerShell Core is based .NET Core which does not include everything we are used to in a full Windows desktop. One of the major limitations that concern many people is the loss of support for graphical tools. There is no support for Windows Forms or WPF in PowerShell Core. Perhaps the biggest lament I hear is the loss of Out-Gridview. I get it. The command can be a useful tool. Now — what if I were to tell you that you could run a command In PowerShell Core and still send the results to the Out-Gridview command you know and love?

Requirements

My solution, which is really not much more than a workaround, assumes you are running a Windows desktop like Windows 10 which means you have access to Windows PowerShell. The premise behind my solution is pretty simple: take the output of a PowerShell Core command and send it to a Windows PowerShell instance running Out-Gridview. As long as you can use Out-Gridview in a Windows PowerShell session, and I can’t image why you couldn’t, then you are all set.

An Out-Gridview Alternative for PowerShell Core

The essential part of my solution is running the PowerShell.exe command to start a new instance. At a prompt if you type powershell /? you’ll see the syntax. I was originally trying to send the PowerShell Core output as an argument for the scriptblock. But there are limitations on the size of the scriptblock. So I took the next best step I could think of. I take the output of the PowerShell core command and serialize it to a file using Export-Clixml. In the PowerShell.exe scriptblock I convert it back using Import-Clixml and pipe the results to Out-Gridview.

I wanted to duplicate the functionality of Out-Gridview as much as possbile. I wanted to be able to display results or display and select. The challenge I encountered was that when simply displaying Out-Gridview, the Windows PowerShell window opens and closes before you can see anything. Using -Passthru provides the experience you expect. My solution was to add a Pause statement at the very end. This keeps the Windows PowerShell window open until you press Enter. And you can still pass objects back to PowerShell Core.

The code for this magic is on Github.

I even added the ogv alias.

You can see I am running PowerShell Core on a Windows platform. I’m pass the results of Get-Process to Out-Gridview, specifying a window title and using -Passthru. This is the same Out-Gridview command.

I can select items and click OK. The results are written back to the PowerShell Core pipeline and stored in $P. You’ll notice the pause message. In the function I temporarily change the host foreground color to make it stand out. I doubt many people are running a yellow background, but if so you’ll need to tweak the function.

Limitations

My solution is not without a few drawbacks. Because I’m using the Clixml cmdlets there is a bit of overhead in serializing and deserializing. Add the disk time, and the time to start the Windows PowerShell process and all that overhead adds up. It isn’t necessarily intolerable, but you need to recognize there is a price to pay. As long as you aren’t trying to send 10K file objects to Out-Gridview I don’t think you’ll complain much. And of course you can always be selective.

I hope a few of you will grab a copy of the function and let me know what you think. Please the the comments section of the Github gist to report any problems, questions or requests. If this was keeping you from PowerShell Core I hope you’ll now reconsider and make the leap with me.

Like this: Like Loading...