5

I'm trying to install new features on a Windows Server 2012 system through PowerShell and C#. I am getting this error back when trying to call the Install-WindowsFeature cmdlet:

The term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program...

I can call other cmdlets though C# and even some of the other new ones. I did a Get-Command and Install-WindowsFeature, Remove-WindowsFeature, and Get-WindowsFeatures are not listed...but about 980 others are.

Why wouldn't I be able to call this cmdlet from my C# program, but when I go to PowerShell it calls it just fine?

runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Install-WindowsFeature");
Collection<PSObject> output = pipeline.Invoke();
4
  • What module is that in? Maybe it is in a module that is not imported by in default with that overload of CreateRunspace. You can add ps modules to the InitialSessionState object that you pass to a different overload of CreateRunspace. Commented Oct 23, 2012 at 0:25
  • It is in the ServerManager module. I've never used the InitialSessionState object, I will have to look into that. Commented Oct 23, 2012 at 1:31
  • @mikez Tried your suggestion, still a no go. string[] modules = { "ServerManager" }; InitialSessionState iss = InitialSessionState.CreateDefault(); iss.ImportPSModule(modules); runspace = RunspaceFactory.CreateRunspace(iss); Commented Oct 23, 2012 at 2:44
  • @Matronix: Were you able to fix this issue? Commented Jul 9, 2019 at 21:03

2 Answers 2

1

Try this command instead of Install-WindowsFeature in your code:

Invoke-Expression "Powershell -Command {Import-Module ServerManager ; Install-WindowsFeature XXXX}"

Sign up to request clarification or add additional context in comments.

1 Comment

Import-Module : The specified module 'ServerManager' was not loaded because no valid module file was found in any module directory.
1

Try compiling in x64. In Visual Studio go to menu ProjectPropertiesBuild - Platform Target = x64.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.