8,330 questions
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 theCredential
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 }