1

Here's the MS documentation concerning Add-Type

So I'm trying to add type through command line:

>powershell Add-Type -TypeDefinition "public class T{public static int Add(int a, int b){return (a + b);}}" -Language CSharp

or

>powershell Add-Type -TypeDefinition "@"""public class T{public static int Add(int a, int b){return (a + b);}}"""@" -Language CSharp

or

>powershell Add-Type -TypeDefinition "@"""public class T{public static int Add(int a, int b){return (a + b);}}"""@" -Language CSharp

or

>powershell Add-Type -TypeDefinition @"public class T{public static int Add(int a, int b){return (a + b);}}"@ -Language CSharp

But whatever I try the execution returns an error. Is it possible to add type by calling the powershell through the cmd console? And then execute the added methods?

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Are you say, you already tried your code in a real editor, ISE, VSCode, and it worked, and make sure it is syntactically correct, then you manually typed it in the console host and it always fails? The question I have is, why are you doing this in an interactive console? Just curious, as this is not a normal thing I've seen anyone do/write about, or need to. I can't think of a use case for that approach, but hey, folks have their reasons. – postanote Apr 23 '20 at 00:11

1 Answers1

1

Here's why you can't do what you are doing and the docs specifically show this.

This punctuation is called a Here String. and it serves a specific purpose.

If you did this in a real PowerShell Editor (ISE, VSCode), note that putting all this on one line generates a syntax error.

Proper here string

$Source = @"
public class T
{
    public static int Add(int a, int b)
    {
        return (a + b);
    }
}
"@

This is not proper (will show syntax error highlighting)...

$Source = @"public class T{public static int Add(int a, int b){return (a + b);}}"@

... because these '@""@' must be on separate lines and fully left aligned and nothing can follow the first one on the same line or be in front of the second one on the same line

This does not show syntax error highlighting:

$Source = @"
public class T{public static int Add(int a, int b){return (a + b);}}
"@

... this can be done in the PowerShell console host but not cmd.exe calling powereshell.exe.

The consolehost will not show syntax errors as well as the editors, if at all in this case.

Anytime you are typing code in the consolehost that requires a CRLF or the like, you have to type it that way. Lastly, doing this in cmd.exe would never show PowerShell syntax errors, until you try and run it.

This is all about fully understanding PowerShell syntax and punctuation.

The Complete Guide to PowerShell Punctuation

postanote
  • 15,138
  • 2
  • 14
  • 25
  • 1
    No worries. Glad it was useful. Just remember, anything you do in cmd.exe you can do in powershell.exe and more of course. There are a few things to come to terms with relative to the environments, but using powershell.exe every day, you'll get use to it. You can call out to cmd.exe or other exes as needed from PowerShell. https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx. Also, look at Windows Terminal. https://www.youtube.com/results?search_query=windows+terminal and VSCode - https://www.youtube.com/results?search_query=VScode+powershell – postanote Apr 23 '20 at 17:40