10

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.

5
  • @thumbmunkeys The SE network is supposed to be a resource that shows up in search engine results. An existing resource is not a reason to not ask it on SO. See FAQ: Embrace the non-Googlers and FAQ: How should you respond to “Give me a fish” / RTFM questions? Commented Apr 9, 2014 at 10:03
  • @thumbmunkeys Imagine you're looking for something and you arrive at a question on SO. There's a comment saying "the answer can be found by googling for this and that" with no further info. There are few things on the internet that piss me off more than such situations. Commented Apr 9, 2014 at 10:05
  • @Stijn: I agree, but the answer to this question would be quite lengthy and does not fit the stackoverflow format IMO. Look at the given answer from Marc Wittman, it is as good as my recommendation to use a search engine. Commented Apr 9, 2014 at 10:12
  • "I want to write C# script using Notepad++ and execute the code using PowerShell." This seems clear enough to me. The referenced article, while definitely related, would have the OP writing C# as a PowerShell string. Bill, this should be enough to get you started: function Invoke-CSharp([string]$Path, [string[]]$argList) { $cs = Get-Content $Path -Raw $type = Add-Type -TypeDefinition $cs -PassThru -ErrorAction Ignore $type::Main($argList) } Invoke-CSharp 'C:\Program.cs' 'arg1', 'arg2', 'arg3' Commented Apr 10, 2014 at 6:53
  • @thumbmunkeys This question is very clear and has an exact concise answer, shouldn't be closed! Commented Sep 27, 2016 at 13:30

1 Answer 1

9

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, I was watching a youtube video today where the presenter writes C# code in text editor, then goes to Powershell and executes "scripts app.csx" something like that. I am wondering whether Powershell could be used to run/execute C# code this way. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.