Running powershell command on remote machine

Neil Burton 20 Reputation points
2024-03-25T15:10:14.03+00:00

I have run the following command successfully on a computer locally, can this be run on remote computers by adding the computer name? I've tried a few different ways but it doesn't seems work

Disable-WindowsOptionalFeature -FeatureName Internet-Explorer-Optional-amd64 -Online -NoRestart

Many thanks

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2024-03-25T15:32:43.01+00:00

    To run PS commands remotely you must do a couple of things:

    • You must enable remote PS on the target machine first. This is potential security risk so do so at your own risk. It should be enabled by default on Windows Server but not client machines. Refer to Enable-PSRemoting for more information.
    • If you are not on a domain then you may need to allow PS connections through the firewall. Refer to the earlier docs on how to do that as well.
    • Depending upon your PS version and policies the remote machine may require scripts to be signed. Refer to Set-ExecutionPolicy for how to do that.

    To actually run commands remotely you must:

    • Create a remote session in your script to connect to the remote machine using New-PSSession with the ComputerName parameter. Note that you may also need to use the Credential parameter to pass credentials under which to connect unless the local user has permissions on the remote machine as well.
    • Create a script block with the script to run remotely. This requires you specify the session using the Session parameter.
    Invoke-Command -Session $session -ScriptBlock { script-block }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.