Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute C# code using Powershell [duplicate]

Tags:

c#

powershell

Can I write a C# script code and execute it using PowerShell?

Instead of running C# using command line tools provided by Visual Studio, I want to write C# script using Notepad++ and execute the code using PowerShell.

like image 643
Bill Avatar asked Apr 09 '14 09:04

Bill


People also ask

What is C execute?

Whenever a C program file is compiled and executed, the compiler generates some files with the same name as that of the C program file but with different extensions.

Can I run C in CMD?

We usually use a compiler with a graphical user interface, to compile our C program. This can also be done by using cmd. The command prompt has a set of steps we need to perform in order to execute our program without using a GUI compiler.

Where we can execute C code?

Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New. Step 2: Write the C program code. Step 3: Click on Compile or press Alt + F9 to compile the code. Step 4: Click on Run or press Ctrl + F9 to run the code.


1 Answers

Using C# Code in PowerShell

"Customer like scripting languages as it allows them to write custom code without a need to run a compiler or to copy new executables to their production machines which usually requires a more complex approval process than deploying a script file or even to execute the commands within a command shell.

So it would be great if the existing C# code could be reused inside PowerShell without a need to implement it as Cmdlet."

$Assem = (
...add referenced assemblies here...
    )

$Source = @"
...add C# source code here...
"@

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp 

Full example provided in the blog article

https://blogs.technet.microsoft.com/stefan_gossner/2010/05/07/using-csharp-c-code-in-powershell-scripts/

like image 69
Marc Wittmann Avatar answered Oct 13 '22 20:10

Marc Wittmann