Add a .NET type to a session (execute cscript)

Costas

Administrator
Staff member
The Add-Type cmdlet adds the class to the session. Because it's using inline source code, the command uses the TypeDefinition parameter to specify the code in the $Source variable. src

Bash:
$Source = @"
public class TestClass{

    public static int Add(int a, int b)
    {
        return a+b;
    }
    
    public int Multiply(int a, int b)
    {
        return a*b;
    }
}
"@

Add-Type -TypeDefinition $Source

# call of static method
[TestClass]::Add(4,3)

Read-Host -Prompt 'Press enter to exit..';

#instantiate class and call method
$b = New-Object TestClass
$b.Multiply(4,3);

Read-Host -Prompt 'Press enter to exit..';
 

Costas

Administrator
Staff member
Use WPF XAML design file to build a form

https://adamtheautomator.com/powershell-gui/

PowerShell GUI - Howto get started
Create a GUI for PowerShell Scripts
Creating a Custom Input Box
2014 - Build a form in PowerShell
MS - About Classes
Call a PS from resources on windows form

JavaScript:
//reference another script file
//src - https://www.jonathanmedd.net/2015/01/how-to-make-use-of-functions-in-powershell.html
//Note the dot and a space before the reference to the Tools.ps1 file

. C:\Users\jmedd\Documents\WindowsPowerShell\Scratch\Tools.ps1
cls
 
Top