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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With