How to Run PowerShell Commands on Remote Computer


In this blog post, we will walk you through how to run PowerShell commands on remote computer.


To connect to remote computer, we can use Enter-PSSession that will create interactive session or Invoke-Command which is not interactive.

In this context, we have two computers named vm1 and vm2 respectively. The client will be vm1 and we want to kill MS Edge processess in vm2.

Since the computers are not in the same domain, we could use Windows Remote Management (WinRM) for remote communication or remoting.

Below are several prerequisites to use WinRM for remote communication:

  1. Enable Windows Remote Management (WinRM) service on both computers

Set-Service -Name WinRM -Status Running -StartupType Automatic

  1. Add servers we want to connect to TrustedHosts list on client computer

Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value 'vm2'

  1. Enable firewall rule for WinRM to allow other IPs to connect

Set-NetFirewallRule -Name 'WINRM-HTTP-In-TCP' -RemoteAddress Any
Enable-NetFirewallRule -Name 'WINRM-HTTP-In-TCP'

Or you can also do it manually:

1. Open Windows Defender Firewall with Advanced Security
2. Click Inbound Rules
3. Double-click Windows Remote Management (HTTP-In) for the Public profile
4. Click the Scope tab
5. Under Remote IP address, set it to `Any IP Address`
6. Enable Rule
Set-NetFirewallRule -Name 'WINRM-HTTP-In-TCP' -RemoteAddress Any Enable firewall rule for Windows Remote Management (HTTP-In)

Using Enter-PSSession cmdlet

To create session to remote computer, we can use Enter-PSSession and specify the computer name we want to connect as well as the credential (username and password). After connected, we can run the commands.

In this example, we run Stop-Process command to stop Microsoft Edge processes on remote computer. Then to close the session, we use Exit-PSSession cmdlet.

Enter-PSSession vm2 -Credential (Get-Credential)

Stop-Process -Name 'msedge'


connect to remote computer using enter-pssession stop process on remote computer

Using Invoke-Command cmdlet

With this cmdlet, you still can run commands on remote computer but you won’t have interactive session. The commands will be executed in one go in a ScriptBlock.

Invoke-Command -ComputerName vm2 -ScriptBlock { Get-Culture } -Credential (Get-Credential)

You still have to enter credential before the ScriptBlock is executed.

enter credential when using invoke-command using invoke-command to run commands on remote computer


Before running commands on remote computer, we need to establish connection to remote computer (remoting). If the computers are not in the same domain, we could use Windows Remote Management (WinRM). Then, we can use Enter-PSSession or Invoke-Command to execute commands on remote computer.