The type or namespace name ‘automation’ does not exist in the namespace ‘system.management’

I have added the reference Assembly System.Management.Automation in my C# application to run PowerShell scripts from C# project. but I am getting following error during compilation of the code.

Error 30 The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)

This is my C# code:

using System.Management.Automation;
using System.Management.Automation.Runspaces;
//-----------------------------------
static void Main(string[] args)
{
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript("Get-Date");
    Collection<PSObject> results = pipeline.Invoke();
    runspace.Close();
    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        Console.WriteLine(obj.ToString());
    }
}

Fix/Solution

The issue was fixed after I have changed the reference of System.Management.Automation to c:Program FilesReference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll.

I have added the reference Assembly System.Management.Automation from the location “C:Program Files (x86)Reference AssembliesMicrosoftWindowsPowerShell3.0” and my C# projects targets .NET Framework 3.5. Initially I didn’t find the the reference System.Management.Automation.dll when I search the Assemblies in Add Reference window, so that I have added the reference by manual browse option from the location “C:\Program Files (x86)Reference AssembliesMicrosoftWindowsPowerShell3.0”.

Finally I found that the assembly file System.Management.Automation.dll (under the folder PowerShell 3.0) is supported only .NET Framework 4.0, so that have changed the reference Assembly to c:Program FilesReference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll to fix this issue.


Advertisement

2 thoughts on “The type or namespace name ‘automation’ does not exist in the namespace ‘system.management’”

Leave a Comment