How to Run Commands on Remote Computers with PowerShell

How to Run Commands on Remote Computers with PowerShell

Running commands locally on the remote computers is a common task many IT admins have to do every day. Here's how you can do that in PowerShell.

It’s sometimes not enough to use DCOM, RPC, and other remote protocols to perform tasks on remote computers. To run commands on the actual remote computer requires a different approach. An admin must create a session of some kind and send commands across the network which are then consumed by the remote computer.

Since this is an article about PowerShell and not about telnet (you’re not using telnet, are you?), we’re going to stick with a technology called PowerShell Remoting. PowerShell Remoting is a feature of PowerShell that was introduced in Windows PowerShell v2. It allows users to open, manage and close sessions on remote computers and interact with them just as if they were sitting on the local console.


To establish a session called a PSSession with PowerShell requires that the remote computer has PowerShell Remoting enabled and listening on port 5985 (HTTP) or 5986 (HTTPS) with all of the usual firewall exceptions. Depending on how the user wants to establish the connection, some client configuration may be necessary as well. There are lots of different configurations a user may find themselves in, but for this article, we’re going to assume the local and remote computers are both in the same Active Directory domain. In this context, PowerShell Remoting will use Kerberos to authenticate, and we won’t need to pass alternate credentials to the commands.

Assuming that the remote computer has PowerShell Remoting enabled, let’s first try out running simple commands on a remote computer. As a test to ensure the code is running on the remote computer, I always like to use the hostname command which returns the name of the computer.

There are two ways we can run remote commands via PowerShell Remoting; we can issue commands interactively or non-interactively. Interactively means we’ll need to be at our computer to run the commands physically. Let’s start off by showing a non-interactive example.

Learn how to automate IT tasks with PowerShell. Download this eBook. 

To run remote commands non-interactively, we use the Invoke-Command command. This command has a ComputerName parameter that allows us to specify a computer to run on the command. We also have a ScriptBlock parameter where we’ll encapsulate the commands we intend to run on the remote computer. You can see below how this works in action. I’m running the hostname command locally on my computer and then running it remotely with Invoke-Command. You can see that it returns different results.

PS C:\> hostname
PS C:\> Invoke-Command -ComputerName SRV1 -ScriptBlock {hostname}

You’ve now run your first remote command! Anything can go into the ScriptBlock parameter. Below, I’m creating an if/then statement inside of the $scriptblock as a test to ensure the code is running remotely.

PS C:\> $scriptblock = { if ((hostname) -ne 'MACWINVM') { 'This is a remote computer' } else { 'We are local' }}
PS C:\> Invoke-Command -ComputerName SRV1 -ScriptBlock $scriptblock
This is a remote computer

You may not have noticed but the Invoke-Command command created a lightweight session in the background to run that command and when the command was complete it tore it down. This is more obvious when you choose to run commands interactively using the Enter-PSSession command.

The Enter-PSSession command allows you to log into a remote session, run commands, get output and work just as if you were typing commands into the local console. To do this though, first, you need to establish a session. One way to do that is to use the ComputerName parameter. Notice below that I’ve entered a remote session and have a new PowerShell prompt that’s prepended by the computer name the session is running in.

PS C:\> Enter-PSSession -ComputerName SRV1
[SRV1]: PS C:\>

At this point, I can run any commands I’d like, and they will be executed on the remote computer. We don’t need to use Invoke-Command to do this. We simply run the commands as they are. When we’re done, we have to close the session. To do that, we use the exit keyword which disconnects us from the session and closes it.

Related: Installing Chocolatey Packages Remotely With PowerShell

You can see below once we type exit and hit Enter, we’re immediately brought back to our local console.

[SRV1]: PS C:\>exit
PS C:\>

PowerShell Remoting is a convenient way to execute commands on remote computers. As long as the remote computer is configured for PowerShell Remoting, which is a one-time setup, there is minimal, if any, setup needed on the client computer for this to work. Especially, if in an Active Directory environment, the process is seamless and a great way to prevent having to sneakernet all around the office just to run scripts and commands on all of your computers! Don't stop here; read our eBook, "How to Automate Using PowerShell," for other PowerShell tricks.


Related Posts

Comments are disabled in preview mode.
Loading animation