Embed / Invoke / Host PowerShell in C#

Hi guys. I’m a budding C# developer who is learning PowerShell to make more powerful apps that can touch workstations and servers. I’ve been trying with limited success to embed some PowerShell in my C# code. I know this is possible because I’ve seen old remarks about it. Trouble is, after Googling for hours and hours and hours all I can seem to find are some really old (like 8-10 year old) posts with outdated code. If anyone can point me to a more recent blog or forum post I would much appreciate it.



This is possible and people are still doing this. Below are two links which you can refer and are not outdated.



Thanks. I’ve seen the links you shared. They appear to be amoung the very vew links on the Internet. I’m giving the Microsoft page a closer look. I might be able to cobble something together with that information. Appreciate the tips.


Hi Burt,

Here is some code that I used to PowerShell on C#. Please note that it’s using the System.Automation class (which I think is out of date)


Hope that helps!


Use this as a starter effort.

Using PowerShell Core in .NET Core 2.0 Console Application

Calling C# code in Powershell and vice versa

Calling Powershell from C# in dotnet core.

See also:
The Zen of Powershell - PowerShell for the C# dev - Peter Ibbotson

Connect to Microsoft Exchange PowerShell within C#

How to execute PowerShell script (ps1 file) from C# code

How to call PowerShell cmdlets from C# in Visual Studio

Thanks guys! I’m picking my way through this information, but it’s been very useful already. Will report back…