How can I run a PowerShell script from a remote computer? Are there any basic instructions for getting Powershell remoting to work through WinRM? In this tutorial we’ll walk you through the steps to run PowerShell commands on a remote computer.
Let’s say your local PC is called “PC01” and you’re going to use PowerShell remoting to manage a remote server called “Server01“. Before getting started, make sure the network profiles on both PCs are set to either Domain or Private. If your network profile is set to Public, you can change it to Private by following this article.
Part 1: Enable PowerShell Remoting
- On the remote server, open the PowerShell console as Administrator and run the following command which will start the WinRM service and create a firewall to allow incoming connections.
Enable-PSRemoting -Force
- Next, you need to configure the TrustedHosts setting on the remote server so it will trust your local PC “PC01“. Type the following command and hit Enter.
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "PC01" -Force
- Restart the WinRM service to apply your new settings.
Restart-Service WinRM
Part 2: Configure Your Local PC
- On the local PC, open the PowerShell with admin rights and start the WinRM service using this command:
Start-Service WinRM
- Next, you can add the remote server “Server01” to the TrustedHosts list with the following command, so your local PC will also trust the remote server.
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "Server01" -Force
- Run the following command to restart the WinRM service:
Restart-Service WinRM
Part 3: Test PowerShell Remoting
- Now when both computers have been configured, you can start a remote session by running the following PowerShell command on your local PC. Remember to replace “Tom” with the name of your remote server’s administrator account.
Enter-PSSession -ComputerName Server01 -Credential Tom
- Enter the administrator password when prompted. Now, the session is established, all commands that are being entered in the PowerShell console are executed on the remote computer.
- To end the interactive session, type this command:
Exit-PSSession
That’s all!