tags:

views:

115

answers:

4

Is it possible to create a string in a C# .exe and use this string in PowerShell?

+1  A: 

Yes. You can use .NET classes in powershell, for instance you can use:

PS C:\> [System.Environment]::GetLogicalDrives()

when dealing with a static method.

Rik
+4  A: 

Yes.

Your question is quite vague - are you using a powershell script to create an object from the c# assembly, then using that object? If so, simply set a property to the string you want and access it as normal from powershell.

Or, if it's a static method, you can just call it

   [My.Assembly.Type]::GetSomeString()

If you are running the c# exe, you can simply output it with writeline and powershell will pick it up:

.\myexe.exe > $someVar

Or, you can write it as a cmdlet, and your c# code can participate in the pipeline.

Philip Rieck
This is the only answer that is related to "C# .exe" :) Voted up. The others are obviously missing that.
stej
A: 

Powershell uses the .net Framework under the covers, all objects are CLR object

Iain
+3  A: 

In the event you are hosting PowerShell within a C# app and you want to access a string variable defined in C# from the PowerShell script that you run, here's how to do that:

string foo = "some string defined in C# to be used in PowerShell";
runspace.SessionStateProxy.SetVariable("foo", foo);

You can then reference this variable in your PowerShell script as $foo.

Keith Hill