Run a C# .cs file from a PowerShell Script

powershell call c# dll
call c# function from powershell
run powershell script from c#
sitecore powershell call c#
create dll from powershell script
powershell c# console writeline
c# powershell output to string
run c code in powershell

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.

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.

Weekend Scripter: Run C# Code from Within PowerShell, public static class CDRemoteTimeout On my system I have a csharptemplate.​ps1 file which I can quickly copy and adjust to run my C# code if required For the above listed C# example the final powershell script – would  When you pass a string to the PowerShell executable it treats it as the Command switch, which is pure PowerShell script. If you want it to explicitly treat it as a script file can use the -File parameter. It just happened to work with paths without spaces, since you can call a script that way.

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);
    }
}

Executing C# code using PowerShell script – Random IT Utensils, How to run C# .Net code in PowerShell | Use existing code and even DLLs | Explanation with PowerShell code. public class Permissions. {. public static System The code will be compiled at the beginning of the script and written to memory. After that Especially when it comes to file-system-operations. To add PowerShell scripting to your program, you first have to add a reference to the System.Management.Automation assembly. The SDK installs this assembly in the C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0 directory. Then, you have to add the following ' using ' statements to import the required types:

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)

Using CSharp (C#) code in Powershell scripts, The SDK installs this assembly in the C:\Program Files\Reference Assemblies\​Microsoft\WindowsPowerShell\v1.0 directory. Then, you have to add the following '  How to execute PowerShell script or cmdlets from C# code? - Program.cs

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

How to use .Net code in PowerShell (C# and DLL integration), Creating project in Visual Studio; Select Class Libray template; Name the tool and click OK Executing C# code directly in PowerShell script using Add-Type Create TestTool.ps1 file, then define $Assem as bellowing:  I tried running a script localwindows.ps1 from C# using the following Code : PSCredential credential = new PSCredential(userName, securePassword); WSManConnectionInfo

How to run PowerShell scripts from C#, C# scripting files are just plain-text files with the extension of .csx; With #r "path of dotnet-script initCreating VS Code launch configuration file Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option. Select the New option to create a new empty .ps1 file. Write a new or paste the script

Executing C# code in PowerShell – nguyentoanuit, Enough with history of Powershell, let's see how we can leverage C# (Another $MyCode = @" public class Calc { public int Add(int a,int b) { return a+b; } but the real power comes while we run powershell command in C#,  There is this C#-Script project that enables you to run .cs files as a script (vbs-style) It seemed to me that you just want to execute that single file, so C#-Script should do it without the need to learn any compilings. But anyway, you'd still need the .NET Framework. Grab that first.

Hitchhiker's Guide to the C# scripting, 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  Create the script in a plain text editor such as Notepad and save with a .PS1 file extension (for example, myscript.ps1 ). Run the script by entering the full path to the script ( c:/scripts/myscript.ps1 ), or if it’s in the current directory, prefix it with a period followed by a backslash

Comments
  • 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?
  • 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.
  • So why don't you call/run the compiled executable instead? If it takes parameters, you can give it parameters.
  • This example might help Using CSharp (C#) code in Powershell scripts
  • you should read about scriptCS. It allows you to run *.cs files with PowerShell. You may find more detailed information here
  • Get-Content may need -Raw