# Running powershell within a Windows Forms application

**URL:** <https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916>\
**Category:** PowerShell Help\
**Created:** [March 25, 2022, 2:01pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916 "2022-03-25T14:01:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomasso](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/thomasso/32/1799_2.png) [@Thomasso](https://forums.powershell.org/u/Thomasso)\
**Post date:** [March 25, 2022, 2:01pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/1 "2022-03-25T14:01:55Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [March 25, 2022, 2:49pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/2 "2022-03-25T14:49:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kira-Shadow](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Kira-Shadow](https://forums.powershell.org/u/Kira-Shadow)\
**Post date:** [March 25, 2022, 8:43pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/3 "2022-03-25T20:43:32Z")

</div>

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:-

```auto
using System.DirectoryServices.AccountManagement;

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

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [March 25, 2022, 9:22pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/4 "2022-03-25T21:22:18Z")

</div>

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

> **[PowerShell Universal - Ironman Software](https://ironmansoftware.com/powershell-universal)**
>
> A single pane of glass for managing and delegating access to your automation environment. Expose APIs, run scripts and build websites using a single tool.

---

<div class="post-metadata">

**Author:** ![Thomasso](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/thomasso/32/1799_2.png) [@Thomasso](https://forums.powershell.org/u/Thomasso)\
**Post date:** [March 28, 2022, 6:12am UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/5 "2022-03-28T06:12:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Thomasso](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/thomasso/32/1799_2.png) [@Thomasso](https://forums.powershell.org/u/Thomasso)\
**Post date:** [March 28, 2022, 6:13am UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/6 "2022-03-28T06:13:59Z")

</div>

Thanks! That might be the way to go.

---

<div class="post-metadata">

**Author:** ![Thomasso](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/thomasso/32/1799_2.png) [@Thomasso](https://forums.powershell.org/u/Thomasso)\
**Post date:** [March 28, 2022, 6:16am UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/7 "2022-03-28T06:16:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brandoncomputer](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@brandoncomputer](https://forums.powershell.org/u/brandoncomputer)\
**Post date:** [April 24, 2022, 6:55pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/8 "2022-04-24T18:55:59Z")

</div>

install-module powershell-designer

powershell-designer

---

<div class="post-metadata">

**Author:** ![Thomasso](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/thomasso/32/1799_2.png) [@Thomasso](https://forums.powershell.org/u/Thomasso)\
**Post date:** [April 30, 2022, 5:11pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/9 "2022-04-30T17:11:37Z")

</div>

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](https://www.sapien.com/software/powershell_studio)?

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/running-powershell-within-a-windows-forms-application/18916/10 "2024-05-16T20:17:58Z")

</div>


