Running powershell within a Windows Forms application

Hi all.

I’m thinking about making a couple of simple applications using Visual Studio and integrating some of the scripts/queries I have developed for our Active Directory environment.

I have found some very basic blog posts explaining how to do this - for instance from https://foxlearn.com/windows-forms/how-to-execute-powershell-script-in-csharp-444.html, but I don’t find them very informative and when I try to run them II’m getting errors.

I’m wondering whether this is at all the way to go, but I just find it tempting because I now know some powershell and want to utilize this and give my users a couple of gui options to run the commands.

But maybe it’s not at all the way to go and maybe I should try and learn the same stuff and run it from C#?

Which approach are you guys taking when you want to build simple windows applications?

I have used Windows Forms in some of my PowerShell Scripts building them from PowerShell alone. I think using a higher level language like C# would only add complexity. You can do it all in PowerShell. At least that has been my experience.

Personally from experience I wouldn’t suggest mixing languages from a debugging point of view, also the example you used was AD, however for get-aduser,get-adgroup to work you also need RSAT installed (Remote Server Administration Tools), if you use native .net you don’t need that as far as I know:-

using System.DirectoryServices.AccountManagement;

using (var pc = new PrincipalContext(ContextType.Domain, "MyDomainName"))
{
    var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "MyDomainName\\" + userName);
}

Another option would be to use PowerShell Universal and build your interfaces as web pages:

Thanks for your reply!
Yes I stumbled upon some tutorials explaining that this can be build directly from Powershell, and this might be the solution. But I would prefer if I could make an exeutable which users can take and start the program without having to start it from a console.

Thanks! That might be the way to go.

Interesting! I might take a look at that at some point.
However this is another system which one has to learn :slight_smile: My intention was just to make some simple windows desktop apps for my users.

install-module powershell-designer

powershell-designer

Thanks. I will try and install that on Monday when I’m back at my desk.

Anybody have experience with this tool PowerShell Studio | The Most Powerful PowerShell GUI Designer and Script Debugger Available?

I used a script editor from Sapien many years ago, but that was for vbscript.
It worked great but it was not for creating guis.
I wonder if this is good. I will give it a try.