If you want to create an object based on the C# code in a *.cs file, by using PowerShell 3.0 you can use the following C# and PowerShell code:

 

Create a folder "C:\Temp"

 

image

 

C# MyClass.cs file

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace MyNameSpace
{
    public class MyClass
    {
        public void MyFunction()
        {
            Console.WriteLine("Hello from MyNameSpace.MyClass.MyFunction.");
        }
    }
}
    

 

PowerShell Execute_MyNamespace_MyClass_MyFunction.ps1

 Add-Type -Path "C:\Temp\MyClass.cs"
$labeler = New-Object MyNameSpace.MyClass
$labeler.MyFunction()
pause 

Execute

image

 

image

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.