<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

How to Run Commands on Remote Computers with PowerShell

Adam Bertram| October 23 2018

| IT insights, PowerShell, Automation, Sysadmin

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
MACWINVM
PS C:\> Invoke-Command -ComputerName SRV1 -ScriptBlock {hostname}
SRV1

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!

Topics: IT insights, PowerShell, Automation, Sysadmin

Leave a Reply

Your email address will not be published. Required fields are marked *

THIS POST WAS WRITTEN BY Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.