none
The type or namespace name 'PowerShell' could not be found (are you missing a using directive or an assembly reference?) RRS feed

  • Question

  • Hi Guys,

    I'm just starting to get my feet wet in #C#and powershell. As part of my learning, I'm trying to run a powershell .ps1 script from C#. The content of the powershell script is just Importing an ActiveDirectory module and create a new AD user (New-ADUser). 

    My machine is windows 2016 with Powershell version 4.0 installed. 

    Steps That I've Done:

    1. Create a ConsoleApp project in visual studio 2019 with .net 4.5 as the target Run time.

    2. Download powershell package from nuget (https://www.nuget.org/packages/System.Management.Automation).

    3. Reference the System.Management.Automation Assembly (from nuget) to my project.

    4. use the " using System.Management.Automation" .

    5. Testing to create a new Powershell instance using : 

    PowerShell ps = PowerShell.Create()

    But I got an error saying : 

    (1,1): error CS0246: The type or namespace name 'PowerShell' could not be found (are you missing a using directive or an assembly reference?)

    (1,17): error CS0103: The name 'PowerShell' does not exist in the current context

    > 

    I guess I won't be able to continue further until I resolve this issue.

    Did I miss anything here ? I've searched the internet and I could not find any answer to this.

    Any help would be appreciated.

    Thanks,


    Monday, September 9, 2019 10:39 AM

All replies

  • In addition to the System.Management.Automation assembly add a using statements for the System.Collections.Objectmodel, which is used later for execution output.

    "keep calm and start coding!" (etabetaweb)

    Thursday, December 26, 2019 6:22 PM
  • I guess the reason is that you are using PowerShell Core in .NET Framework application. What you need to do is create .NET Core application to use it.
    Monday, December 30, 2019 9:23 AM