Overview

13 Remote control: one-to-one, and one-to-many

PowerShell remoting provides a shell-wide, protocol-agnostic way to run commands on remote systems, enabling you to execute cmdlets that may not even be installed on your local machine. On Windows, remoting uses WS-MAN via the WinRM service; on macOS and Linux, it uses SSH, and PowerShell registers as a subsystem so PSRP traffic is routed correctly. Administrators typically enable remoting on the computers that receive commands, often enterprise-wide, and should account for prerequisites such as PowerShell version, authentication and trust, listeners, firewall rules, and network profiles. When commands run remotely, results are serialized to XML and deserialized back into objects on the client, which means you receive read-only snapshots rather than live, method-capable objects.

For one-to-one remoting, you establish an interactive session to a single host using Enter-PSSession, choosing -HostName for SSH or -ComputerName for WinRM. The prompt changes to indicate the remote context, commands run under the supplied credentials, and you exit with Exit-PSSession; if you close the window, the session ends automatically. Profile scripts on the remote machine do not load in these sessions, and you should avoid “second hop” chaining unless you understand the implications and have configured credential delegation appropriately. While in a 1:1 session, you’re effectively typing on the remote console, so objects are live and actions like stopping processes behave as expected on that remote system.

For one-to-many remoting, Invoke-ScriptBlock sends the contents of a script block to multiple computers in parallel, with a configurable throttle limit and the convenience of sourcing host lists from files. Careful punctuation and nesting of braces matter: place as much filtering and action logic inside the remote script block as possible so that each target does its own work, minimizing network traffic and avoiding issues with deserialized, methodless objects. This approach is crucial when performing actions (for example, retrieving and then stopping a process) that must occur on the remote machines. Common pitfalls include mixing local processing with remote results, misunderstanding serialization limits, and environment or policy settings that block listeners or firewall exceptions; persistent sessions and deeper endpoint configuration can further streamline repeat operations.

Graphical user interface, text, application, email Description automatically generated
The relationship between SSHD, and PowerShell
The relationship between WinRM, WS-MAN, endpoints, and PowerShell

 Lab answers

  1. Enter-PSSession Server01

The nano process will launch, but there won’t be any interactive process either locally or remotely. In fact, run this way, the prompt won’t return until the nano process ends—although an alternative command to launch it is Start-Process nano.

The Notepad process will launch, but there won’t be any interactive process either locally or remotely. In fact, run this way, the prompt won’t return until the Notepad process ends—although an alternative command to launch it is Start-Process Notepad.

  1. Invoke-ScriptBlock –scriptblock {Get-Process } -HostName Server01,Server02 -UserName yourUser | Format-Wide -Column 4
  2. Invoke-ScriptBlock -scriptblock {get-process | sort VM -Descending | Select-first 10} –HostName Server01,Server02 -UserN
  3. Invoke-ScriptBlock -scriptblock { Get-ChildItem ~/* | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 10} -HostName (Get-Content computers.txt) -UserName yourUser
  4. Invoke-ScriptBlock –scriptblock $ -Server01,Server02 -UserName yourUser

FAQ

What is PowerShell remoting, in a nutshell?It lets you run commands on a remote computer while only the results are returned to your console. You’re effectively typing on the remote machine; this works across Windows, Linux, and macOS.
What transport does remoting use on different platforms?Windows typically uses WS-MAN via the WinRM service (HTTP/HTTPS). Linux and macOS use SSH. Windows can also use SSH if you install and configure OpenSSH. PowerShell remoting (PSRP) runs over either transport.
What do I need before I can remote?- PowerShell v7.1 or later on both sides - Remoting enabled on the target - Network/firewall access to the listener (WinRM or SSH) - Ideally, machines in the same or trusted domains (otherwise follow help about_remote_troubleshooting)
When do I use -HostName vs. -ComputerName?-HostName uses SSH. -ComputerName uses WinRM/WS-MAN. PowerShell won’t guess the protocol, so you must pick the correct parameter for the transport you’ve configured.
How do I do one-to-one remoting?Use Enter-PSSession and Exit-PSSession. For SSH: Enter-PSSession -HostName server -UserName user. For WinRM: Enter-PSSession -ComputerName server -UserName domain\user. The prompt changes to show the remote machine; Exit-PSSession ends it. Note: profile scripts on the remote machine don’t run in these sessions.
What is the “second hop” (remote chaining) problem?It’s when you start a remote session and then try to remote again from within that session, creating a chain that’s hard to manage and may fail due to authentication constraints. Avoid remoting from inside a remote session unless you’ve specifically addressed second-hop auth. End the session and initiate a new one from your local console instead.
How do I run the same command on many machines at once?Use Invoke-ScriptBlock with a list of targets and a -ScriptBlock. Example: Invoke-ScriptBlock -HostName srv1,srv2 -ScriptBlock { Get-Process pwsh }. PowerShell talks to multiple machines in parallel (default up to 32; tune with -ThrottleLimit). You can pass names from a file via -HostName (Get-Content .\computers.txt).
Why do remote results act differently from local objects?Remote results are serialized to XML and deserialized on your machine, producing “snapshot” objects without most methods. That’s why you should perform actions (like Stop-Process) inside the remote -ScriptBlock, and only send back data you want to view or save.
What are common pitfalls and errors?- Public network profile blocks WinRM firewall rules; switch to a private/work profile or use SSH - Not running PowerShell as admin when enabling WinRM on Windows - Expecting your remote session to load remote profiles (it won’t) - Splitting a workflow across multiple Invoke-ScriptBlock calls; each call starts fresh - Misplacing braces so filtering happens locally instead of remotely
How can I make remoting efficient and safe?Do as much work as possible on the remote side: filter, sort, and take actions inside the -ScriptBlock; only format or export locally. Keep parallelism reasonable with -ThrottleLimit. Secure SSH (prefer key auth) and ensure WinRM/SSH listeners and firewall rules are configured properly.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free