Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi , I'm trying to execute C# code in Power Shell 2.0 .

My problem is for the references at the beginning of the code:
$referencingassemblies = (


code= @"
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

   public class Program
        static void Main()
        {  /* the code witch works*/

Add-Type -ReferencedAssemblies $referencingassemblies -TypeDefinition $code -Language "CSharpVersion3" -IgnoreWarnings

Powershell gives me an error saying that The type or namespace Linq doesn't exist. I have Visual studio express 2013.

How can add or import correctly both References at the same time?or what I'm doing wrong?
Updated 17-Apr-14 20:35pm
DamithSL 17-Apr-14 12:39pm    
try with Add-Type -TypeDefinition $code -Language CsharpVersion3 -IgnoreWarnings
Sergey Alexandrovich Kryukov 17-Apr-14 13:38pm    
No. The problem is simple: the assembly should be loaded. Please see my answer.
notjustme 17-Apr-14 14:26pm    
I actually explained how to solve this to the original poster about a month ago.

Adding "-ReferencedAssemblies "C:\Here\Are\My\dlls\System.Xml.dll" to the Add-Type command will make the code work. This question is a duplicate really.
Kinna-10626331 18-Apr-14 2:32am    
I tried but still don't working because I can' add correctly more than one referenecesAssemblies at the same time
notjustme 18-Apr-14 4:02am    
I've shown you how to do this in your original post.

I don't know such namespace. Here is the thing: Power Shell is the interpretive system; and you need to loads some additional assembly which is not loaded by default. As everything in interpretive system, it needs to be loaded "during runtime", dynamically. How? I'll demonstrate how to load an assembly from GAC, if the (default) versions automatically matching the .NET versions your version of Power Shell is targeted to. For example, this is how you can load and use Forms:
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

All the code written after this line is executed can use System.Windows.Forms.Forms, System.Windows.Forms.Application and other declaration required to create windows UI working in PowerShell (I do have such application). Same thing about WPF or nearly any other .NET FCL libraries not loaded by default.

The same way you can load the extra assemblies you need for your functionality. If your assembly is not in GAC, you can load it in some other way. Look at all the methods named [System.Reflection.Assembly]::Load:[^].

This mechanism is pretty non-trivial and specific to Power Shell as interpretive system.

Share this answer
PowerShell gives you the correct error message: namespace System.Xml.Linq does not exist.

PowerShell v2 uses CLR 2.0, and System.Xml.Linq is not part of it yet.

Update to PS v4, it uses CLR v4.0, which has that namespace.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900