How PowerShell Remoting Works in V7

powershell hero img

PowerShell Remoting in V7

PowerShell on its own is a robust and versatile language that can help you accomplish any task at hand. Combine it with the ability to run remotely on other computers and it becomes unparalleled as a scripting language.

If you are wondering how PowerShell remoting works in version 7 – the latest release from Microsoft – you’re not alone.

In this article you will dive into how PowerShell remoting works and the different ways it can be utilized, wrapping up with a couple of ways to troubleshoot your PowerShell remote sessions.

How PowerShell Remoting Works in V7

PowerShell utilizes the Windows Remote Management (WinRM) Framework behind the scenes to deliver the remoting feature. WinRM may sound familiar since it is also the management framework that powers PowerShell Desired State Configuration (DSC) and Windows Management Instrumentation (WMI).

As a result of using WinRM, PowerShell remoting operates over WinRM’s default ports of TCP 5985 and 5986 for HTTP and HTTPS communications, respectively. In order to get started using PowerShell Remoting, there are a couple of prerequisites you’ll need to be conscious of before you get too far.

System Requirements

In order to use PowerShell Remoting, the following must be in place on the local and on the remote computer:

  • PowerShell version 3.0 or later
  • Microsoft .NET Framework 4 or later
  • Windows Remote Management 3.0

These should come out of the box on Windows 8 and Windows Server 2012 and later, but it’s always a good idea to check before getting started.

User Permissions

You’ll be running PowerShell on a local and remote computer, with the potential to change the computer configuration, modify files, and much more. It follows that by default you’ll need to have administrator credentials in order to operate PowerShell remotely. You can either be a part of the Administrators group on both the local and remote machines, or you can provide the administrator credentials when prompted.

Like with most things Microsoft, this default behavior can be changed but it is strongly discouraged to weaken the security of the system. If you would still like to learn more about the topic, you can check out this link on PowerShell Session configuration.

How to Use PowerShell Remoting in V7

There are three ways to use PowerShell Remoting in version 7. First, is via the ComputerName parameter which ships with some cmdlets. The second is to use the Invoke-Command cmdlet. Lastly, you can open an interactive PowerShell session which allows you to directly control PowerShell on the remote computer.

The ComputerName Parameter

The simplest way to employ PowerShell Remoting is to use a cmdlet with the ComputerName parameter. This allows you to utilize PowerShell Remoting without the need for a session or any special configuration.

One such cmdlet that we can use with the ComputerName parameter is Restart-Computer. This command will, of course, restart the computer on the local or on a remote machine. Although the “-ComputerName” parameter isn’t required, if you specify a remote computer it will utilize WinRM to send the command to the desired machine.

There are many common cmdlets that will allow you to take advantage of this functionality. Since everyone’s PowerShell environment is slightly different, listing every cmdlet which can be run remotely would be quite cumbersome. You can, however, find the cmdlets on your own! In order to find a list of cmdlets that you can use which let you use the ComputerName parameter, simply run this in your local PowerShell terminal:

PS> Get-Command | where { $_.parameters.keys -contains "ComputerName" -and $_.parameters.keys -notcontains "Session"}

Invoke-Command

The next way to utilize PowerShell Remoting is with the cmdlet Invoke-Command. This cmdlet will run PowerShell on the local or remote computer and, since it uses a PowerShell session, it has the capability to return all output from that command. This output includes errors, making it incredibly useful to administrators.

When using Invoke-Command remotely, you have to specify the remote computer name on which you want PowerShell to run. After that, you can specify a file path for a PowerShell script on your local machine, as you can see below.

PS> Invoke-Command -ComputerName remote -FilePath C:\Scripts\myscript.ps1

Alternatively, if you wish to type the PowerShell script directly into the terminal, you can use the command as shown below to send a script to the remote computer in order to be executed.

PS> Invoke-Command -ComputerName remote -ScriptBlock { Get-Process }

The above command would run the cmdlet Get-Process on the computer “remote” and return the results. Although there are many more ways to use Invoke-Command, this should set you on the right path to running single commands or scripts remotely. Remember, you can always use Get-Help to find more examples and learn more about each of these cmdlets.

Interactive PowerShell Session

The most “high level” way to use PowerShell Remoting is to create and manage PowerShell Sessions on your remote computers. This is akin to SSH in that you’re able to use your local terminal to run commands on a remote machine, returning all input directly to the console. In order to start a session on a remote computer, the following can be entered into your local terminal.

PS> Enter-PSSession remote

Thankfully, since PowerShell is designed to be as user-friendly as possible, in order to exit the PowerShell Session, you can simply enter Exit-PSSession into the terminal and you’ll be removed from the remote session. This does place you back in your local terminal which you can then use to move on to other things.

Entering and exiting a session in an interactive way is fun, but it isn’t as scalable as masters of automation tend to like. One way that you can still run remote commands without an interactive session is to establish a persistent session and pass-through commands to the session.

Here’s an example with some code. Start by creating a variable and assigning the new sessions to it.

PS> $sessions = New-PSSession -ComputerName remote1, remote2

This creates an object which we can use in conjunction with Invoke-Command in order to run scripts and other commands remotely. You can pass it into Invoke-Command like so:

PS> Invoke-Command -Session $sessions -ScriptBlock {$p = Get-Process}

Now if we want to search for and stop a specific process on all of the remote machines, we could do something like the following. For the example “chrome” will be the stopped process.

PS> Invoke-Command -Session $sessions -ScriptBlock {$p | Where $_.Name -eq “chrome” | Stop-Process}

Summary

There are many different ways to utilize PowerShell Remoting in version 7. You can run an individual command with no session needed. You can jump into
an interactive session to have the full might of PowerShell at your fingertips. You can even use Invoke-Command to scale your code out to hundreds or even thousands of machines.

No matter which way you choose to use remoting in the latest version of PowerShell, surely you will agree that this robust toolkit can get the job done.