r/PowerShell • u/Ckarini • Jul 07 '19
Tutorial on creating Powershell applications using Visual Studio
https://domruggeri.com/2019/07/06/creating-extensive-powershell-gui-applications-part-1/19
u/StartingOverAccount Jul 07 '19 edited Jul 07 '19
OK so this is one those thing I've wondered about a few times. Why would anyone create a GUI using Powershell (for a production task)? I see these posts every once in a while in forums and it just doesn't make sense. This tutorial is with Visual Studio. If you have VS why not just use C# or (shudder) VB? You'll get a much nicer UI, built in error handling, mult-threading (if needed).
This is one of those things just because you can, should you.
(yeah I've setup a couple forms on PS just for the fun factor)
Edit: The wondering is more out of curiosity. I don't have strong opinions for or against it just there are better ways to create a UI
5
u/Ckarini Jul 07 '19
I've wondered this myself, when learning how to do this. I'm not too great with Visual Basic or C#, but I understand PowerShell. Login prompts, PowerShell-specific commands, APIs, these are all things I already know how to do in PowerShell, and not in other languages. So, my goal was to allow the other PowerShell scripters to create an application without learning a new programming language.
Also, there are many ways to set up and display the UI which can look just as nice as any C# form. And, I'll be covering Error-Handling in an upcoming article- when we convert this to an exe, the write-warning and write-error commands will do the work for us. And as for the multithreading, we can do that as well with Start-Job.
3
u/StartingOverAccount Jul 07 '19
Fair enough.
To clarify a bit I wasn't implying you can't do any of that in PS. Just the amount of effort required between PS and other languages to accomplish this task.2
u/Ckarini Jul 07 '19
Yeah, these things may be easier to do in other languages, but I only know PowerShell, so that's what's easiest for me.
3
u/Onslaught262 Jul 07 '19
Yeah, I don't know any of those other languages. I'd like to learn C#, specifically because PowerShell is derived from it, but I don't know it now. Tools like this are very helpful for guys (and gals) like me.
15
u/thankski-budski Jul 07 '19
Speaking for myself, the domain I work in has PowerShell readily available and it is trivial to leverage Windows Forms or XAML GUI's, combined with the simplicity of PSRemoting and CIMSessions makes it an attractive approach, albeit with a 'quick and dirty' feel.
The sheer amount of bureaucracy I would have to wade through to even begin to develop and deploy an executable means for most use cases, it just would not be worth it. It would take several months and a strong business case, whereas with a PowerShell GUI, I can just plonk it into a shared area and give people a shortcut.
I do agree that it might not always be the best tool for the job, alas I seem to spend my life doing things using obscure methods because for one reason or another I just can't do them the proper way.
Although, I would imagine that most enterprise domains are not this restrictive, so I'm probably an edge case.
4
u/dextersgenius Jul 07 '19
The sheer amount of bureaucracy I would have to wade through to even begin to develop and deploy an executable means for most use cases, it just would not be worth it. It would take several months and a strong business case, whereas with a PowerShell GUI, I can just plonk it into a shared area and give people a shortcut.
This is exactly my situation. We also have Applocker enabled across all environments that we manage (including our own devices where we don't have admin access), so we can't just plonk an exe somewhere and expect it to work.
1
2
u/northendtrooper Jul 09 '19
I work in the govt sector and they basically block any and all compiled code, including .exe's. Which leave very little left. My job entails a lot with Active directory and machine scripts to repair broken software. We've used .hta's in the past but today's world with run-spaces and multi-threading is a god send for 10000s of objects to be created/moved/removed/updated/etc. Powershell just fits the bill.
4
u/piorarua Jul 07 '19
This looks great. I spent a long time trying to get PowerShell to work with one of my projects. Wish this was there when I started! Looking forward to going through this tutorial!
5
u/Ckarini Jul 07 '19
Thanks! Yeah, I've gone through a lot of different solutions for writing up GUIs- PoshGUI, Sapien, literally writing the form code myself, and I feel like I've finally got it down to a science. I've also had to accumulate knowledge of how to work with all the controls from various places, so I'm hoping to combine all of that into one spot for others to take advantage of. Thanks for reading!
4
Jul 07 '19
Great article. I suppose it depends on how complex you need a GUI to be. I actually learned this method a while ago (thanks to a PluralSight course) and it quickly became not worth the time and hassle. I find that many of my scripts, if they do have a use case for a GUI, require something simple. Just enough for a non-powershell expert to use with the help of a quick README.
For example, I just built a new database migration tool that is somewhat specific to my company and our environment. It also automates the SQL high availability config. We're handing this utility off to some other teams so they can simplify notify us and do this themselves. Some of these users aren't seasoned PowerShell folks, so plugging in up to 8 different parameters, then entering a credential is a little tedious. Using the AnyBox module, I built some very basic GUIs, didn't have to touch visual studio, and it works great. It gives them a few options if they want to start at various points in the process, then opens a parameter window which makes it like filling out a form. They can review and edit any of their params before submitting. This module also gives another layer of controlling user input. If any of you need SIMPLE GUIs... Try this module.
2
7
u/Catmeum Jul 07 '19
What a great and in-depth guide to starting to use a GUI in Powershell! I didn't even know that was possible. Looking forward to the next Tutorial! !remindme 30 days
8
u/get-postanote Jul 07 '19 edited Jul 07 '19
It's been possible for a very long time.
Good to see another article on tthe topic, but there are add on tools for this use case as well.
Resources...
Learning GUI Toolmaking Series
https://foxdeploy.com/series/learning-gui-toolmaking-series
Part I – Creating PowerShell GUIs in Minutes using Visual Studio – A New Hope
Part II – Deploying PowerShell GUIs in Minutes using Visual Studio
https://foxdeploy.com/2015/04/16/part-ii-deploying-powershell-guis-in-minutes-using-visual-studio
Buy and use the PowerShell Pro tools for Visual Studio/PowerShell/VSCode, for this use case.
https://marketplace.visualstudio.com/items?itemName=ironmansoftware.powershellprotools
Youtube
'visual studio powershell'
https://www.youtube.com/results?search_query=visual+studio+powershell
visual studio gui for powershell
https://www.youtube.com/results?search_query=visual+studio+gui+for+powershell
There are also legal limits to what you can and can't do with Visual Studio Community Edition. Make sure you read the eula to avoid the potential legal issues.
3
Jul 07 '19
Also note that AFAIK making GUIs isn’t available in PowerShell Core.
They’re fun to make though can be tedious. I’ve heard that PowerShell Studio is great for building these tools by simplifying the code construction. I almost bought it myself but it’s really expensive and you have to pay again every year.
3
u/Reverent Jul 07 '19
PowerShell core works fine with electron, which is what I use to construct my guis anyway.
1
1
u/get-postanote Jul 08 '19
PS7 will with the new hotness in .Net Core for WinForm/WPF.
MS already announced that.
1
u/RemindMeBot Jul 07 '19 edited Jul 08 '19
I will be messaging you on 2019-08-06 16:33:47 UTC to remind you of this link
3 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
4
u/Onslaught262 Jul 07 '19 edited Jul 07 '19
Hey thanks OP! This is extremely useful to me! I was looking for just this type of information. I really love PowerShell, and have just gotten started on Fiverr hoping to sell scripts (no takers yet...). I realize I need to be able to make GUIs to make my scripts/services more attractive. So please continue with this series of articles. I could really use the help. Thanks again.
1
1
1
1
u/idownvotetwitterlnks Jul 07 '19
I havent used Visual Studio, the company I worked for has licenses for Sapien PowerShell Studio. Super easy creating PS GUI and only $400.
As an FYI - I do not work for Sapien.
14
u/[deleted] Jul 07 '19
Op. You're on Upwork aren't you? You should be charging more.
Good work.