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.

Thanks,

Burt

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

https://docs.microsoft.com/en-us/powershell/developer/hosting/adding-and-invoking-commands

https://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

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.

Burt

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)

https://gist.github.com/ZanattaMichael/941c6e2581de23124ec8591abdc2f8be

Hope that helps!

PSM

Use this as a starter effort.

Using PowerShell Core in .NET Core 2.0 Console Application

Calling C# code in Powershell and vice versa
http://executeautomation.com/blog/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…

Burt