Search code examples
powershellremote-desktopinvoke-command

Remote Invoke-Command to Remove-WindowsFeature does not work as module not available


I have created a powershell session from a Windows 10 machine (client) to a Windows Server 2012 R2 machine (server) and try to uninstall a feature:

$session | Invoke-Command -ScriptBlock { Remove-WindowsFeature Server-Gui-Shell }

I receive the error:

Invoke-Command : The term 'Remove-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

If I RDP to the server machine and start powershell, the command is available, but on the client machine it is not and there is no module ServerManager.

This implies that the command I am trying to run at the server needs to somehow be available on the client, which seems bizarre to me.

Have I missed something?


Solution

  • Invoke-Command does not accept value for -Session parameter by pipeline. By pipeline it accept input objects, which would be passed to invoked command:

    12345 | Invoke-Command { "Pipeline input: $input" }
    # Pipeline input: 12345
    

    As your command does not have any target for remote invocation, Invoke-Command invokes command on local computer. Since local computer does not have requested command, it produce error about that.