Friday, July 20, 2018

How do you run a C# Console Application using PowerShell?

How do you run C# Console Application using PowerShell?

Microsoft.PowerShell.Utility module has a cmdlet, Add-Type. Add-Type can be used to execute the code from PowerShell command shell.

The Add-Type has a large number of parameters and using these Add-Type will define and generate the class. If you specify source code, Add-Type compiles the source code and generates an in-memory assembly that contains the .NET Framework types.

Here are some of the parameters of Add-Type from Microsoft documentation here.

Add-Type
   [-CodeDomProvider ]
   [-CompilerParameters ]
   [-TypeDefinition]
   [-Language ]
   [-ReferencedAssemblies ]
   [-OutputAssembly ]
   [-OutputType ]
   [-PassThru]
   [-IgnoreWarnings]
   []


Here is an example of using the source code in a here-string.


Now we use this string in TypeDefinition that takes a String as follows:

Add-Type -TypeDefinition $Source

Now the code is executed using the following:

[Joinstring]::Add("Jay"," Krishnaswamy"

Here is the code in Windows PowerShell ISE.

 

 

No comments: