C# Code inside a PowerShell Script?

Ever wished you could run C# code inside a PowerShell Script? Well you can. Here’s a simple “Hello World” example which is wrapped it inside a PowerShell Here-String.

$SourceCode = @”
using System;

namespace MyTest
{
public class Hello
{
public static void Main()

{
Console.WriteLine(“Hello, World!”);
}
}
}

“@

Add-Type -TypeDefinition $SourceCode -Language CSharp

Now to execute our script we’ll type:

[MyTest.Hello]::Main()

And that’s basically it. So yes, you can run C# Code inside a PowerShell Script!

 
Comments

No comments yet.

Leave a Reply