Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Created November 19, 2019 03:12
Show Gist options
  • Save joshschmelzle/abcf3dcc9ab48ef0362086513914e62c to your computer and use it in GitHub Desktop.
Save joshschmelzle/abcf3dcc9ab48ef0362086513914e62c to your computer and use it in GitHub Desktop.
PowerShell Notes and Cheatsheet

Loading a .cs file directly from PowerShell. This was taken from Knuckle-Dragger's SO answer.

There is no compile of .cs code, just edit the -Path argument to point to your .cs file. I also had to add -Raw when I tried to do said thing outside of this exmaple.

PowerShell file

$source = Get-Content -Path "A:\basic.cs" -Raw
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 for free to join this conversation on GitHub. Already have an account? Sign in to comment