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?
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
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.
Microsoft has already made announcements that GUI stuff is coming.
FYI — Announcing Open Source of WPF, Windows Forms, and WinUI at Microsoft Connect(); 2018
https://blogs.windows.com/buildingapps/2018/12/04/announcing-open-source-of-wpf-windows-forms-and-winui-at-microsoft-connect-2018/#fWo6AguBhyFeAgxg.97
http://www.eweek.com/development/microsoft-open-sources-wpf-windows-forms-and-windows-ui-xaml-library
Also unveiled at the conference was Microsoft’s .NET Core 3 Preview 1 code, which adds support for building apps using WPF, Windows Forms and XAML Islands.
This would bear out to mean VSCode and PSCore soon to have GUI capabilities along with this a big missing piece for many still sticking with Windows PowerShell specifically because of not having this feature set.
I had heard about this and it will be a welcome addition. It will be interesting to see how it is utilized in PowerShell Core.
Can you use do the below in powershell core like you can in windows powershell, so you can serialize the object to a variable rather then to disk? and then use the deserialize method to convert it back.
$newobject = [System.Management.Automation.PSSerializer]::Serialize($object)
That code works in PowerShell Core, but you still end up with too large a scriptblock. That’s why I use the temporary file solution.
Thanks for taking an early leap into PS Core, to help others prepare for the limitations. This is a pretty good workaround. Although, the downside is that you still need an instance of Windows Powershell. which means that you could not perform this workaround on another platform–somewhat negating the usefulness of using Core in the first place.
What do you think would be the drawbacks of exporting the data to CSV, then invoking the item in a spreadsheet viewer? It seems that would work on any platform, and have a similar, though not identical, functionality as Out-Gridview.
I totally agree this is merely a workaround and only for Windows. Long term we’re going to see some type of support for WPF in .NET Core which should eventually make its way into PowerShell Core. And you could certainly go the spreadsheet route. Although that adds another requirement and doesn’t solve the cross platform problem. I’ll look forward to trying out your solution!
Funny thing is the new version of core does have winforms and wpf so this should be revisited
I just upgraded to the most recent version last night so I’ll have to look into this. Even though it is in the new version of .NET Core, I don’t know if it has been addressed in PowerShell.