Welcome to MSDN Blogs Sign in | Join | Help

How To Write a Console Application in PowerShell with Add-Type

Windows PowerShell CTP3 has a lot of very cool things.  CTP2 introduced the Add-Type cmdlet, which allowed you to dynamically compile C# in PowerShell.  It was actually possible to use the CompilerParameters to Add-Type to make a console application, but it wasn't particularly easy.  In CTP3, we've made this a lot easier to do.

There's now an -OutputType parameter for Add-Type.  It can either output a Library (the default), a ConsoleApplication, or a WindowsApplication.  Check out this really quick "Hello World" program built using Add-Type.

Add-Type -OutputType ConsoleApplication -OutputAssembly HelloWorld.exe @"
using System;

public class MyProgram
{
    public static void Main(string[] args) {
        Console.WriteLine("Hello World");
    }
}
"@

Hope this Helps,

James Brundage [MSFT]

Published Saturday, January 03, 2009 12:26 AM by PowerShellTeam
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: How To Write a Console Application in PowerShell with Add-Type

Very Cool .... I was trying to think when would you want to do this ?

Saturday, January 03, 2009 12:13 AM by Chris Federico

# re: How To Write a Console Application in PowerShell with Add-Type

There's no value in marking the class and the Main() method 'public'.

Saturday, January 03, 2009 2:09 AM by Jay Bazuzi

# re: How To Write a Console Application in PowerShell with Add-Type

PowerShell Team is trying to make admins learn C#.

How fearful this plan is!

Saturday, January 03, 2009 6:22 AM by Smith Catar

# re: How To Write a Console Application in PowerShell with Add-Type

@Smith

> PowerShell Team is trying to make admins learn C#.

Absolutely not!

Now we do want to make it super easy to learn C# for those Admins that want to (some will) but we have zero interest in forcing admins to learn C#.

We provide functions like these so that DEVELOPERS (or advanced scripters) can use PowerShell to provide the right abstractions to Admins.

Jeffrey Snover [MSFT]

Windows Management Partner Architect

Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Saturday, January 03, 2009 11:39 AM by PowerShellTeam

# re: How To Write a Console Application in PowerShell with Add-Type

@Jeffrey

It's just a joke, of course:)

Saturday, January 03, 2009 12:05 PM by Smith Catar

# re: How To Write a Console Application in PowerShell with Add-Type

I very quickly run into this little gem of an error....

I'm running v2.0 ctp3 just downloaded!

just copied the string and pasted it to ps...

What did I do wrong!

Unrecognized token in source text.

At line:1 char:72

+ Add-Type -OutputType ConsoleApplication -OutputAssembly HelloWorld.exe  <<<< @"

   + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

   + FullyQualifiedErrorId : UnrecognizedToken

Saturday, January 03, 2009 12:29 PM by lcr

# re: How To Write a Console Application in PowerShell with Add-Type

@ lcr

You did nothing wrong, because the text is not preformatted, here documents @"

"@

End up with an extra non-standard space at the end of the first line, which will often break it (it's a strange copy/paste behavior from Internet Explorer)

Simply delete the last character on the line.

@chris

I think the case of writing a WindowsApplication is more interesting that the case of ConsoleApplication, but one reason you might want to do this is to expose the information in a way that's easier for a command script, VBScript, or JScript to handle.

It's also valuable if systems administrators want to use the power of PowerShell to make a task easier, but want to protect their individual scripts a little more.

Hope this Helps,

James Brundage [MSFT]

Saturday, January 03, 2009 3:27 PM by PowerShellTeam

# Diagnosing Here-Strings With PowerShell_ISE

James Brundage posted a blog entry How To Write a Console Application in PowerShell with Add-Type which

Saturday, January 03, 2009 7:25 PM by Windows PowerShell Blog

# re: How To Write a Console Application in PowerShell with Add-Type

What is a use case for this?

Why would this help an admin to protect their scripts? I could do the same by writing the code in .cs file and running csc.exe.

The assumption seems to be that admins WANT to write C# (or VB.NET for that matter). Is that what you are aiming for?

Sunday, January 04, 2009 2:33 PM by Alexander Riedel

# re: How To Write a Console Application in PowerShell with Add-Type

@Alexander

Speeding up parts of a script would be a use for this, check the 'burn-console' and 'invoke-inline' scripts at: http://www.leeholmes.com/blog/default,month,2005-12.aspx

Monday, January 05, 2009 2:24 AM by Mark Felt

# re: How To Write a Console Application in PowerShell with Add-Type

Performance benefits are one case, but in that case it would be better to compile C# and use it in PowerShell.

Making a console application is a simplified example of the more interesting example, packing a script into any application this way.

Your company (Sapien) makes a gui builder.  You could use this technique to pack what gets created with your gui builder so that the final customer is unaware of its scripted origins.

Friday, January 09, 2009 4:26 AM by PowerShellTeam

# re: How To Write a Console Application in PowerShell with Add-Type

We are doing that, but I don't see how being able to compile C# from within PowerShell does anything for that. You can't compile PowerShell into an assembly. Or am I missing something?

Saturday, January 10, 2009 5:50 AM by Alexander Riedel

# re: How To Write a Console Application in PowerShell with Add-Type

I'm trying to use this with a C# snippet that parses an XML (I know I can do it directly Powershell, but I want to test this out). I'm using CTP3. The problem is that when I try to add-type the snippet it doesn't find the System.Xml namespace. I've tried "-UsingNamespace" with no luck. So, neither of the following works:

Add-Type -TypeDefinition "using System.Xml;"

Add-Type -TypeDefinition "using System.Xml;" -UsingNamespace "System.Xml"

Obviously, the class that I'm using is bigger than that but it triggers the same problems. Why wouldn't this work?

Thanks

Monday, January 12, 2009 12:11 AM by Stephen

# re: How To Write a Console Application in PowerShell with Add-Type

@ alex:

You can compile PowerShell into an assembly (that's why the parameter is -outputAssembly), as well as a console application, as well as a windows application

@ stephen

You don't have the Xml assembly referenced.

I normally use this trick to reference types I've already got:

Add-Type '

Code

' -ReferencedAssemblies ([XML].Assembly)

Each type has an assembly property, and it's easier to use a type you to get the assembly than it is to remember the full assembly names (at lesat for me)

Hope this helps,

James Brundage [MSFT]

Tuesday, January 13, 2009 2:32 PM by PowerShellTeam

# re: How To Write a Console Application in PowerShell with Add-Type

hi,it seems my powershell doesn't support Add-Type cmdlet, when i run the scripts, the shell return some error info, which means can't recognize Add-Type as cmdlet.

Can somebody tell me why this pheno happens?

Friday, January 23, 2009 4:40 AM by "linfei"

# re: How To Write a Console Application in PowerShell with Add-Type

@linfei

Add-Type command was added in Powershell V2.

You need to install PowerShell V2 CTP3 to be able to use it.

Hope this helps,

Vladimir Averkin

Windows PowerShell Team

Friday, January 23, 2009 11:42 AM by PowerShellTeam

# re: How To Write a Console Application in PowerShell with Add-Type

That doesn't compile PowerShell into an assembly (or console or windows application). It compiles the C# in the here string into an assembly, which is a big difference I think.

Am I missing something?

Sunday, January 25, 2009 6:25 AM by Alexander Riedel

# re: How To Write a Console Application in PowerShell with Add-Type

I also have to come back to the speeding things up comment.

If PowerShell is not fast enough for a task at hand and I need to use C#, it would be far more efficient to create an assembly in C#, pre-compile it and use it from there on out rather than emitting the same assembly from with in a script over and over again.

It seems to be just a good way to clutter the place with assemblies.

Please understand that I am really puzzled about this. As a developer I would never choose this way of providing functionality to admins.

Sunday, January 25, 2009 6:47 AM by Alexander Riedel

# re: How To Write a Console Application in PowerShell with Add-Type

RE: Performance.  Embedding the C# in PS has the following benefits:

1) One file to distribute

2) 32/64 bit neutral

3) user has visibility into what the code is doing [e.g. do I trust this code?]

Experiment! Enjoy! Engage!

Jeffrey Snover [MSFT]

Windows Management Partner Architect

Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Sunday, January 25, 2009 12:47 PM by PowerShellTeam

# re: How To Write a Console Application in PowerShell with Add-Type

Nice, now i can compile my scripts and share them with my coworkers and be shure that the scripts work the same way every time.

Thursday, January 29, 2009 9:27 AM by Doru Apostolescu

# re: How To Write a Console Application in PowerShell with Add-Type

Just started to look into this and was trying to compile one of my scripts but I am getting the following error: (repeatedly)

+ Add-Type <<<<  -OutputType ConsoleApplication -OutputAssembly myscript.exe @"

   + CategoryInfo          : InvalidData: (c:\TEMP\__tvf\t...escape sequence:CompilerError) [Add-Type], Exception

   + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

are there limitations or anything special that needs to be done to ensure that things compile?

Friday, February 27, 2009 12:02 PM by Thomas Fischer

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker