To execute PowerShell commands or scripts on a remote computer, you need to create a session. This is also known as PowerShell remoting and it is just like an SSH session to an operating system. There are several ways to create a PowerShell session. I’ll list one of the easiest methods here. These are the required steps.
Step 1. Enabling PowerShell Remoting
Open the PowerShell with administrative privileges on the remote computer and execute the following command:
This command starts the WinRM service and creates a firewall rule to allow incoming connections. The -force option avoids PowerShell to prompt you for confirmation at each step.
Step 2. Configure TrustedHosts
On both computers, configure the TrustedHosts setting so they know each other. Execute the following command on both computers to do so:
Step 3. Restart WinRM Service
Run the following command to restart the WinRM service now:
Step 4. Test the Connection
To test your configuration, execute the following command on the local computer:
For the configuration to be successful, this command should execute without errors and show the information of the WinRM service of the remote computer.
Create a PowerShell Session and Execute Commands
Now when both computers have been configured, you can create a session using the following commands (Execute these commands with elevated privileges):
“Tell me and I forget, teach me and I may remember, involve me and I learn.” ― Benjamin Franklin.
How to Run PowerShell Commands on Remote Computers
PowerShell Remoting allows you to run individual PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.
PowerShell is locked-down by default, so you’ll have to enable PowerShell Remoting before using it. This setup process is a bit more complex if you’re using a workgroup – for example, on a home network — instead of a domain.
Enabling PowerShell Remoting
On the computer you want to access remotely, open a PowerShell window as Administrator – right click the PowerShell shortcut and select Run as Administrator.
To enable PowerShell Remoting, run the following command (known as a cmdlet in PowerShell):
This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections. The -Force part of the command tells PowerShell to perform these actions without prompting you for each step.
If your computers aren’t on a domain – say, if you’re doing this on a home network – you’ll need to perform a few more steps. First, run the Enable-PSRemoting -Force command on the computer you want to connect from, as well. (Remember to launch PowerShell as Administrator before running this command.)
On both computers, configure the TrustedHosts setting so the computers will trust each other. For example, if you’re doing this on a trusted home network, you can use this command to allow any computer to connect:
To restrict computers that can connect, you could also replace the * with a comma-separated list of IP addresses or computer names.
On both computers, restart the WinRM service so your new settings will take effect:
Testing the Connection
On the computer you want to access the remote system from, use the Test-WsMan cmdlet to test your configuration. This command tests whether the WinRM service is running on the remote computer – if it completes successfully, you’ll know that WinRM is enabled and the computers can communicate with each other. Use the following cmdlet, replacing COMPUTER with the name of your remote computer:
If the command completes successfully, you’ll see information about the remote computer’s WinRM service in the window. If the command fails, you’ll see an error message instead.
Executing a Remote Command
To run a command on the remote system, use the Invoke-Command cmdlet. The syntax of the command is as follows:
Invoke-Command -ComputerName COMPUTER -ScriptBlock < COMMAND >-credential USERNAME
COMPUTER represents the computer’s name, COMMAND is the command you want to run, and USERNAME is the username you want to run the command as on the remote computer. You’ll be prompted to enter a password for the username.
For example, to view the contents of the C:\ directory on a remote computer named Monolith as the user Chris, we could use the following command:
Invoke-Command -ComputerName Monolith -ScriptBlock < Get-ChildItem C:\ >-credential chris
Starting a Remote Session
Use the Enter-PSSession cmdlet to start a remote PowerShell session, where you can run multiple commands, instead of running a single command:
Enter-PSSession -ComputerName COMPUTER -Credential USER