PowerShell Remoting Techniques for Server Management

PowerShell is a powerful server management tool, providing administrators with a wide range of capabilities to manage remote systems. Here are three PowerShell remoting methods.

Brien Posey

July 5, 2023

5 Min Read
hands typing on laptop keyboard
Alamy

While it may be tempting to think of PowerShell as a programming language, its primary function is server management. As such, Microsoft has embedded various remoting options into PowerShell for this purpose.

Since server management is often done remotely rather directly at the server console, PowerShell’s remoting capabilities allow administrators to manage network servers from a distance. More importantly, these remoting capabilities can also serve as a tool for performing bulk management tasks.

As previously noted, PowerShell offers several options for managing remote systems. The choice of method just depends on the specific objective you aim to achieve.

Enabling PowerShell Remoting

Before you can use PowerShell to remotely manage a system, you must first enable PowerShell remoting on that system. You can do this by following these steps.

  1. Open an elevated PowerShell session on the machine you want to manage.

  2. Enter the Enable PS-Remoting cmdlet in the local PowerShell session on the machine you will be managing remotely (see Figure 1).

Additionally, you will need to open the appropriate firewall ports for PowerShell remoting. Port 5985 is used for normal remoting connections, while Port 5986 is used for secure remoting connections. Typically, using the Enable-PSRemoting command will automatically open the required firewall ports in Windows. However, if your firewall allows public connections or if you are using a third-party firewall, you will need manually configure the firewall settings.

Related:How To Perform Admin Tasks From a Standard User Account

Shows how to enable PowerShell remoting using the command Enable-PSRemoting

Remoting 1

Figure 1. This is how you enable PowerShell remoting.

Technique #1: Using a Single Command

The simplest form of PowerShell remoting involves directing a single command to run against a remote system. This technique is straightforward because many (possibly most) PowerShell cmdlets support a parameter called “ComputerName.”

To use this technique, enter the cmdlet just as you would for local execution and append the -ComputerName parameter followed by the name or IP address of the remote system. You can see what this looks like in Figure 2.

Shows the Get-Process cmdlet executed against a remote machine in PowerShell

Remoting 2

Figure 2. The Get-Process cmdlet is being run against a remote machine.

There are two main drawbacks to consider when using this technique. First, it is primarily designed for running a single command against a remote system. Although there are workarounds, it is generally easier to use the other remoting techniques that I explain below.

The second limitation is that not all commands supporting the ComputerName parameter also support the use of credentials (although some do). If the Credential parameter is unavailable, the account that you are currently signed in with on your local machine must have the required permissions on the remote machine.

Technique #2: Using Invoke-Command for Advanced Remoting

If using the ComputerName parameter with a PowerShell cmdlet is insufficient for your task, you can employ the Invoke-Command cmdlet instead.

The Invoke-Command cmdlet allows you to send a command to a remote system, even if the command doesn’t natively support the ComputerName or Credential parameter.

To use this technique, follow these simple steps.

  1. Enter the Invoke-Command cmdlet, followed by the -ComputerName parameter and the -Credential parameter (if required).

  2. Use the -ScriptBlock parameter to specify the command you want to execute on the remote system.

As an example, let’s say you want to run the Get-Process cmdlet on a remote system with an IP address of 192.168.0.4. The commands would be as follows:

$Cred=Get-CredentialInvoke-Command -ComputerName 192.168.0.4 -Credential $Cred -ScriptBlock {Get-Process}

In the above example, the first command creates a variable called $Cred to store a username and password combination. The second command runs the cmdlet specified within the script block (in this case, Get-Process).

An example of using Invoke-Command in PowerShell

Remoting 3

Figure 3. This is what the example looks like in PowerShell.

Interestingly, the Invoke-Command cmdlet can be run against multiple remote machines. Simply enter multiple computer names, separated by commas.

It’s important to note that this technique allows for running multiple commands within the script block, not just one cmdlet.

Technique #3: Using an Interactive Session

The third technique is to create an interactive session. With this technique, your PowerShell session is directly connected to the remote machine, allowing you to send commands to that computer.

To establish a remote session, follow these steps:

  1. Use the New-PSSession command, providing the ComputerName and Credential parameters. This command will display a session number.

  2. Type the Enter-PSSession cmdlet, followed by the session number, to connect to the remote session. 

You can see what this looks like in Figure 4. Notice that the PowerShell prompt changes to reflect the remote machine’s name (in this case, “DC”).

The New-PSSession and Enter-PSSession cmdlets are used to establish a remote session in PowerShell

Remoting 4

Figure 4. You can use the New-PSSession and Enter-PSSession cmdlets to establish a remote session.

To end the remote session:

  1. Enter Exit-PSSession to return to your local PowerShell session.

  2. Use the Remove-PSSession command, followed by the session number, to close the connection to the remote machine.

See Figure 5 for the process of terminating a session.

The process of terminating a session in PowerShell

Remoting 5

Figure 5. This is how you terminate a remote session.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like