

PS C:\> Invoke-Command -ComputerName SRV1 -ScriptBlock $scriptblock

PS C:\> Invoke-Command -ComputerName SRV1 -ScriptBlock You can see that it returns different results.

I’m running the hostname command locally on my computer and then running it remotely with I nvoke-Command. You can see below how this works in action. We also have a ScriptBlock parameter where we’ll encapsulate the commands we intend to run on the remote computer. This command has a ComputerName parameter that allows us to specify a computer to run on the command. To run remote commands non-interactively, we use the Invoke-Command command. Let’s start off by showing a non-interactive example. Interactively means we’ll need to be at our computer to run the commands physically. There are two ways we can run remote commands via PowerShell Remoting we can issue commands interactively or non-interactively. 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. In this context, PowerShell Remoting will use Kerberos to authenticate, and we won’t need to pass alternate credentials to the commands.Īssuming that the remote computer has PowerShell Remoting enabled, let’s first try out running simple commands on a remote computer. 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.

Depending on how the user wants to establish the connection, some client configuration may be necessary as well. 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.
