On this page

PowerShell Remoting with WinRM: Complete Guide

Updated on June 17, 2026

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

PowerShell Remoting is the standard mechanism for executing commands and scripts on remote Windows systems. Built on WinRM (Windows Remote Management)—Microsoft's implementation of the WS‑Management protocol—it enables administrators to manage individual machines or entire fleets using the same commands they would run locally. When a remote command is issued, the local PowerShell session contacts the target machine through WinRM, authenticates the credentials, spawns an isolated PowerShell host on the remote system, executes the command in that remote environment, and returns structured PowerShell objects to the local session. This object‑based approach distinguishes PowerShell Remoting from traditional remote shells and makes it the recommended method for managing Windows systems at scale.


Enabling PowerShell Remoting​

On Windows Server 2012 and higher, PowerShell Remoting is enabled by default. On Windows 10 and Windows 11, it must be enabled manually.

To enable remoting, open PowerShell as Administrator and run:


Enable-PSRemoting -Force

What this command does: The Enable-PSRemoting cmdlet configures the computer to receive PowerShell remote commands sent via WS‑Management technology. It performs the following operations:

The -Force parameter suppresses confirmation prompts, which is useful in automation scripts.

Note: On client versions of Windows (Windows 10/11), Enable-PSRemoting can also enable remoting when the computer is on a public network by using the -SkipNetworkProfileCheck parameter. However, the default firewall rule for public networks only allows remoting connections from within the same subnet; allowing all connections requires explicitly changing that rule.


Verifying Remoting Connectivity​

Before attempting remote execution, verify that the target machine is reachable and WinRM is functioning.

Checking the WinRM Service Status​


Get-Service WinRM

This command confirms whether the WinRM service is running on the local machine.

Testing Remote Availability​


Test-WSMan -ComputerName SERVER01

The Test-WSMan cmdlet sends a request to the WinRM service on the specified computer. If WinRM is running and properly configured, the command returns WS‑Management details (such as the protocol version and product vendor). If it fails, WinRM is either not running, blocked by a firewall, or misconfigured. This command does not run PowerShell commands remotely—it only confirms that the WinRM service is responding.


Executing Commands Remotely with Invoke‑Command​

The primary cmdlet for running remote commands is Invoke-Command. It sends a script block to one or more remote computers and returns the results to the local session.

Running a Simple Command Remotely​


Invoke-Command -ComputerName SERVER01 -ScriptBlock { Get-Service }

Explanation: The Get-Service command executes on SERVER01, not on the local machine. The output is returned as service objects that can be further processed locally. The script block runs in the remote machine's environment—paths such as C:\Windows refer to the remote system.

Running a Command on Multiple Machines​


Invoke-Command -ComputerName SERVER01, SERVER02 -ScriptBlock {

    Get-Process powershell

}

The script block executes independently on each machine. Results from all computers are combined into a single output stream, with the PSComputerName property indicating the source of each result.


Persistent Remote Sessions (PSSessions)​

While Invoke-Command is suitable for one‑off commands, many administrative tasks require maintaining state across multiple commands. PowerShell supports this through PSSessions, which behave like persistent remote PowerShell consoles.

Creating a Session​


$session = New-PSSession -ComputerName SERVER01

This establishes a persistent connection to the remote machine. The session preserves variables, imported modules, the working directory, and loaded data across commands.

Running Commands Inside a Session​


Invoke-Command -Session $session -ScriptBlock { Get-Date }

Commands run in the session using the -Session parameter instead of -ComputerName. The session maintains state between invocations.

Entering an Interactive Remote Shell​


Enter-PSSession -Session $session

This command enters an interactive session where the prompt changes to [SERVER01]: PS C:\> . Every command typed thereafter executes on the remote machine until the session is exited:


Exit-PSSession

Closing the Session​


Remove-PSSession $session

This terminates the remote PowerShell host and frees resources on the remote machine.


Passing Local Variables to Remote Commands​

When a script block executes remotely, it runs in the remote environment and does not automatically inherit local variables. To pass local values, use the $using: scope modifier.


$serviceName = "Spooler"



Invoke-Command -ComputerName SERVER01 -ScriptBlock {

    Get-Service -Name $using:serviceName

}

Explanation: The variable $serviceName is evaluated locally, and its value is transmitted to the remote session. Without $using:, the remote script block would look for a variable named $serviceName on the remote machine, which would not exist.


Running Scripts Remotely​

PowerShell can execute entire script files on remote machines without copying the file to the target.


Invoke-Command -ComputerName SERVER01 -FilePath "C:\Scripts\Cleanup.ps1"

Explanation: The -FilePath parameter reads the script from the local machine and sends its contents to the remote computer for execution. The script runs on the remote machine exactly as if it were executed locally there, with access to the remote machine's file system, registry, and other resources.


Authentication and Credential Handling​

By default, PowerShell Remoting uses the credentials of the currently logged‑in user. When connecting to machines outside the current security context, explicit credentials are required.

Prompting for Credentials​


$cred = Get-Credential

This displays a dialog box for entering a username and password.

Using Credentials in a Remote Command​


Invoke-Command -ComputerName SERVER01 -Credential $cred -ScriptBlock { hostname }

The -Credential parameter passes the credential object to the remote session. The remote command runs under the security context of the supplied user. By default, only members of the Administrators group on the remote machine can establish a PowerShell Remoting session.


Security Considerations​

Encryption​

Regardless of whether HTTP or HTTPS is used as the transport protocol, WinRM always encrypts all PowerShell Remoting communication after initial authentication. This means that even with HTTP (port 5985), the actual command traffic is encrypted.

Authentication Methods​

PowerShell Remoting supports multiple authentication mechanisms:

Process Isolation​

WinRM runs as a service under the Network Service account and spawns isolated processes running as user accounts to host PowerShell instances. An instance of PowerShell running as one user has no access to a process running as another user, providing strong isolation between sessions.

TrustedHosts​

When a client is not joined to a domain, or when NTLM authentication is used, the target computer must be added to the WinRM TrustedHosts list.


Set-Item WSMan:\localhost\Client\TrustedHosts -Value "SERVER01"

Explanation: This command configures the local WinRM client to trust the specified computer. TrustedHosts accepts wildcards—for example, *.contoso.com trusts all machines in a domain, and * trusts all machines (not recommended for production). Note that computers in the TrustedHosts list might not be authenticated, which introduces a security risk.


Secure Remoting with HTTPS​

For environments requiring additional security, WinRM can be configured to use HTTPS on port 5986. HTTPS provides:

Configuring an HTTPS Listener​

To configure WinRM to use HTTPS:

  1. Obtain or generate a server certificate for the WinRM host. The certificate must have a subject name that matches the hostname and must be trusted by the client.
  2. Install the certificate in the Local Machine certificate store.
  3. Create an HTTPS listener:

winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{

    Hostname="SERVER01.contoso.com"

    CertificateThumbprint="<thumbprint>"

}

Explanation: This command creates a listener that accepts HTTPS connections on any IP address (Address=*). The Hostname must match the certificate's subject name. The CertificateThumbprint identifies the certificate to use for SSL encryption.

  1. Open the firewall for port 5986 (HTTPS).

Once HTTPS is configured, Invoke-Command works with the same syntax, but connections use the secure channel automatically.


Practical Administrative Examples​

Restarting a Remote Service​


Invoke-Command -ComputerName SERVER01 -ScriptBlock {

    Restart-Service -Name Spooler

}

Collecting Disk Usage from Multiple Servers​


Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {

    Get-PSDrive -PSProvider FileSystem

}

Explanation: Get-Content servers.txt reads a list of computer names from a text file. Invoke-Command runs the Get-PSDrive command on each server, returning disk usage information for all file system drives.

Deploying a Script to Many Machines​


Invoke-Command -ComputerName (Get-Content servers.txt) `

    -FilePath "C:\Deploy\InstallUpdates.ps1"

This runs the local script InstallUpdates.ps1 on every machine listed in servers.txt.

Checking WinRM Service Status on Multiple Machines​


Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {

    Get-Service WinRM | Select-Object Name, Status, StartType

} | Format-Table PSComputerName, Status, StartType

Explanation: This command retrieves the status and startup type of the WinRM service from each remote machine and formats the output as a table with the computer name.


Troubleshooting Common Issues​

Access Denied or Authentication Errors​

Symptoms: Enter-PSSession fails with "Access Denied" or "The WinRM client cannot process the request."

Causes and solutions:


Set-Item WSMan:\localhost\Client\TrustedHosts -Value "SERVER01"

Firewall Blocking Connections​

Symptoms: Test-WSMan times out or fails to connect.

Solution: Ensure the firewall allows inbound traffic on port 5985 (HTTP) or 5986 (HTTPS). The default firewall rule is named WINRM-HTTP-In-TCP.

Service Principal Name (SPN) Conflicts​

Symptoms: Error 0x80090322 when connecting to a remote server.

Cause: The HTTP SPN for the remote computer is already registered to another service account.

Solution: Force PowerShell Remoting to include the port number in the SPN (WSMAN/<server>:5985), which is owned by the computer account and not subject to conflicts.

Double‑Hop Issue​

Symptoms: A remote command that tries to access another remote resource (e.g., a file share or another server) fails with an access denied error.

Cause: By default, credentials are not delegated from the first remote machine to a second remote machine.

Solution: Use CredSSP authentication or configure Kerberos delegation to enable the double‑hop scenario.


Summary​

PowerShell Remoting through WinRM provides a robust, secure, and scalable framework for remote administration:

These capabilities form the foundation of enterprise‑level automation with PowerShell, enabling administrators to manage hundreds or thousands of machines from a single console.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Remote Management

Next

Invoke-Command