33

I have a PowerShell V2 script that moves some files around and installs some services. However I would like to call and run a .cs file about halfway through the PowerShell Script. I have found plenty of articles on calling PowerShell from C# but none the opposite way around. I would just like the C# file to run once then continue running the PowerShell script.

If anyone could point me in the direction of an article explaining how to accomplish this or if you know yourself and could help it would be greatly appreciated.

5
  • 1
    You don't "run a .cs file"... it isn't a script. It'll be part of a larger project, a console or wpf app maybe. Can you give any more details on the file and what it's a part of? Commented Jul 21, 2014 at 14:57
  • Yes sorry, it is a Console Application that edits an XML file that I have. It mostly just changes text in the XML file, nothing too complicated. It only has one class called class1. Commented Jul 21, 2014 at 15:02
  • 2
    So why don't you call/run the compiled executable instead? If it takes parameters, you can give it parameters. Commented Jul 21, 2014 at 15:04
  • This example might help Using CSharp (C#) code in Powershell scripts Commented Jul 21, 2014 at 15:56
  • 1
    you should read about scriptCS. It allows you to run *.cs files with PowerShell. You may find more detailed information here Commented Jul 22, 2014 at 8:01

5 Answers 5

58

I saw no reason why we couldn't run a .cs file directly from PowerShell, so I took Keith's snip and added the missing Get-Content parts to do literally what the OP asks for. No need to compile your code, just edit the -Path argument to point to your .cs file.

$source = Get-Content -Path "A:\basic.cs"
Add-Type -TypeDefinition "$source"

# Call a static method
[BasicTest]::Add(4, 3)

# Create an instance and call an instance method
$basicTestObject = New-Object BasicTest
$basicTestObject.Multiply(5, 2)

Basic.cs

public class BasicTest
{
    public static int Add(int a, int b)
    {
        return (a + b);
    }

    public int Multiply(int a, int b)
    {
        return (a * b);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Get-Content may need -Raw
I copied this wrong too. You don't need -Raw if you use their code and stringify: "$source"
Get-Content with -Raw and simply passing the result as a variable should be more reliable.
It's obvious if you know, but if there's a namespace in the .cs you'll need to include that when calling from powershell: [Full.Namespace.BasicTest]::Add(4, 3)
Love that this runs from a floppy drive `A:`
34

You can use Add-Type to compile C# code and add it to the current PowerShell session. Then you call the C# code like you would any other .NET framework code. This is an example from the man page on Add-Type:

PS C:\>$source = @"
public class BasicTest
{
  public static int Add(int a, int b)
    {
        return (a + b);
    }
  public int Multiply(int a, int b)
    {
    return (a * b);
    }
}
"@

PS C:\>Add-Type -TypeDefinition $source
PS C:\>[BasicTest]::Add(4, 3)
PS C:\>$basicTestObject = New-Object BasicTest
PS C:\>$basicTestObject.Multiply(5, 2)

1 Comment

I tried this Add-Type example in Powershell Core v. 7.1.4 (the Original Poster's question was about Powershell V2) running on MacOS, and it still works great today.
8

You're looking for the wrong thing. Put your C# into an assembly, and call its public classes, functions and methods from PowerShell, just like you would call the .NET Framework from Powershell.

If you really want to compile and run C# source from PowerShell, see Weekend Scripter: Run C# Code from Within PowerShell.

Comments

4

A .cs file can be directly read.

Add-Type -Path 'program.cs'

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-5.1

Comments

0

If you want, you can run c# script from *.bat file:

  1. create c# script (e.g. Program.cs) with public static void Main() method
  2. create *.bat (e.g. Run.bat) file and place this code:
@ECHO off
Echo Please run as administrator
setlocal
cd /d %~dp0
set csDir=%cd%\Program.cs
powershell ^
set-executionpolicy remotesigned; ^
$source = Get-Content -Raw -Path %csDir%; ^
Add-Type -TypeDefinition "$source"; ^
[Program]::Main()
  1. Run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.