PowerShell Remoting & WS-Management (1/3)
PowerShell Remoting runs PowerShell commands on remote computers and brings the results back as objects. It runs on WinRM (WS-Management over HTTP 5985 / HTTPS 5986) and is built into Windows 10/11 and Server with PowerShell 5.1 and 7. Part 1 covers enabling remoting, interactive and disconnected sessions, session configurations, CredSSP and the WSMan basics.
237.1 Why is it used?
- Manage many servers from one admin PC without RDP.
- Results come back as (deserialized) objects you can filter and sort.
- Disconnected sessions let long jobs keep running on the server while you go home.
- Constrained endpoints (session configurations) limit what a remote user can do.
237.2 How it works
- Step 1
1.Admin PC asks the domain controller for a Kerberos ticket for Server01
- Step 2
2.Enter-PSSession becomes a WS-Management SOAP message
- Step 3
3.Sent to the WinRM listener on Server01, HTTP 5985 (POST /wsman)
- Step 4
4.Command runs in the wsmprovhost.exe process on Server01
- Step 5
5.Result returns as XML and becomes objects on the admin PC
- Step 6
6.With -UseSSL the same request goes over HTTPS 5986
| Transport | Port | Notes |
|---|---|---|
| HTTP | 5985 | Default listener; in a domain Kerberos protects the session |
| HTTPS | 5986 | -UseSSL; needed with NTLM/IP addresses unless TrustedHosts is set |
237.3 On this PC (real, read-only)
Get-Command Connect-PSSession,Connect-WSMan,Enable-PSRemoting | Select Name,Source(Get-Help Enable-PSRemoting).SynopsisGet-Service WinRM | Select Name,Status,StartTypeName Source---- ------Connect-PSSession Microsoft.PowerShell.CoreConnect-WSMan Microsoft.WSMan.ManagementEnable-PSRemoting Microsoft.PowerShell.CorePS> (Get-Help Enable-PSRemoting).SynopsisEnable-PSRemoting [-Force] [-SkipNetworkProfileCheck] [-WhatIf] [-Confirm] [<CommonParameters>]PS> Get-Service WinRM | Select Name,Status,StartTypeName Status StartType---- ------ ---------WinRM Stopped Manual- WinRM Stopped/Manual: this PC is not receiving remote commands.
- A PC that only sends commands does not need Enable-PSRemoting.
237.4 Enable-PSRemoting and Disable-PSRemoting
- Starts WinRM and sets startup type Automatic.
- Creates a listener on every IP address.
- Adds a WS-Management firewall exception.
- Registers and enables the Microsoft.PowerShell session configurations.
- Restarts WinRM.
Get-NetFirewallRule -Name 'WINRM*' | Select-Object -Property NameEnable-PSRemoting -SkipNetworkProfileCheck -ForceDisable-PSRemoting -ForceName----WINRM-HTTP-In-TCP-NoScopeWINRM-HTTP-In-TCPWINRM-HTTP-Compat-In-TCP-NoScopeWINRM-HTTP-Compat-In-TCPPS> Disable-PSRemoting -ForceWARNING: Disabling the session configurations does not undo all the changes made by the Enable-PSRemoting or Enable-PSSessionConfiguration cmdlet. You might have to manually undo the changes by following these steps: 1. Stop and disable the WinRM service. 2. Delete the listener that accepts requests on any IP address. 3. Disable the firewall exceptions for WS-Management communications.- Run it as admin on the computer that RECEIVES commands; -SkipNetworkProfileCheck allows client Windows on a public network.
- Windows Server 2012 and later have remoting enabled by default.
- Disable-PSRemoting only blocks remote access to the endpoints; WinRM, the listener and firewall rule stay on. Loopback still works with New-PSSession -ComputerName localhost -EnableNetworkAccess.
237.5 Common parameters
| Parameter | What it does |
|---|---|
| -ThrottleLimit | Max concurrent connections (0 or default = 32) |
| -Confirm / -WhatIf | Ask first / only show |
| -AsJob | Run the remote command as a background job |
237.6 Enter-PSSession, Exit-PSSession, Get-PSSession
Enter-PSSession -ComputerName Server01Get-Process powershell > C:\ps-test\Process.txtexitdir C:\ps-test\Process.txt$s = New-PSSession -ComputerName Server01Enter-PSSession -Session $sExit-PSSession$sPS C:\> Enter-PSSession -ComputerName Server01[Server01]: PS C:\> Get-Process powershell > C:\ps-test\Process.txt[Server01]: PS C:\> exitPS C:\> dir C:\ps-test\Process.txtGet-ChildItem : Cannot find path 'C:\ps-test\Process.txt' because it does not exist.PS C:\> $s = New-PSSession -ComputerName Server01PS C:\> Enter-PSSession -Session $sServer01\PS> Exit-PSSessionPS C:\> $sId Name ComputerName State ConfigurationName-- ---- ------------ ----- -----------------1 Session1 Server01 Opened Microsoft.PowerShell- The file was written on Server01, so a local dir cannot find it.
- A New-PSSession session stays Opened after exit; a temporary -ComputerName session is closed.
- Other options: -Port, -Credential, -UseSSL, -Authentication, -ConfigurationName, -VMName, -ContainerId, -RunAsAdministrator.
- Only one interactive session at a time.
Get-PSSession -ComputerName Srv1, Srv2, Srv30 -Name ITTaskId Name ComputerName State ConfigurationName Availability-- ---- ------------ ----- ----------------- ------------ 1 ITTask Srv1 Disconnected Microsoft.PowerShell None 2 ITTask Srv2 Opened Microsoft.PowerShell Available 3 ITTask Srv30 Opened Microsoft.PowerShell Available- Without parameters Get-PSSession lists only sessions created in the current session; -ComputerName also lists sessions stored on that server.
- -State All | Opened | Disconnected | Closed | Broken filters them.
237.7 Disconnected sessions
- Step 1
1.Office PC: New-PSSession -Name ITTask on Server01
- Step 2
2.Invoke-Command starts a backup job (Job2 Running)
- Step 3
3.Disconnect-PSSession: connection cut, session and job keep running on Server01
- Step 4
4.Home PC: Get-PSSession shows ITTask Disconnected
- Step 5
5.Connect-PSSession: State Opened, Job2 Completed
$s = New-PSSession -ComputerName Server01 -Name ITTask -ConfigurationName ITTasksInvoke-Command -Session $s -ScriptBlock { Start-Job -FilePath \\Server30\Scripts\Backup... }Disconnect-PSSession -Session $s -OutputBufferingMode Drop -IdleTimeoutSec 60*60*15Id Name State HasMoreData Location Command-- ---- ----- ----------- -------- -------2 Job2 Running True Server01 \\Server30\Scripts\Backup... Id Name ComputerName State ConfigurationName Availability-- ---- ------------ ----- ----------------- ------------ 1 ITTask Server01 Disconnected ITTasks None- -OutputBufferingMode Drop keeps the script running when the output buffer fills; the default Block suspends it until you reconnect.
- -IdleTimeoutSec: how long the disconnected session lives (min 60, default 2 hours; here 15 hours).
# evening, from the home PCGet-PSSession -ComputerName Server01 -Name ITTask$s = Connect-PSSession -ComputerName Server01 -Name ITTaskInvoke-Command -Session $s -ScriptBlock {Get-Job} 1 ITTask Server01 Disconnected ITTasks None 1 ITTask Server01 Opened ITTasks Available Id Name State HasMoreData Location Command-- ---- ----- ----------- -------- -------2 Job2 Completed True Server01 \\Server30\Scripts\Backup...- From another computer reconnect by -Name or -InstanceId; -Id does not work there.
237.8 Session configurations (endpoints)
Enable-PSSessionConfiguration -Name MaintenanceShell, AdminShellDisable-PSSessionConfiguration -Name Microsoft* -ForceGet-PSSessionConfiguration -Name Microsoft*Get-PSSessionCapability -ConfigurationName Endpoint1 -Username 'CONTOSO\User' -FullName PSVersion StartupScript Permission---- --------- ------------- ----------microsoft.powershell 5.1 BUILTIN\Administrators AccessAll...microsoft.powershell32 5.1 BUILTIN\Administrators AccessAll...- An endpoint decides which PowerShell environment a remote user gets.
- Enable/Disable-PSSessionConfiguration: admin, local computer only; -NoServiceRestart avoids the WinRM restart.
- Get-PSSessionCapability shows the commands one user may run on a constrained endpoint.
237.9 CredSSP and the double hop
- Step 1
1.Your PC -> ServerB: PSSession with Kerberos (hop 1)
- Step 2
2.Command on ServerB opens a share on ServerC (hop 2)
- Step 3
3.Your credentials are not passed on: ServerC says Access is denied
- Step 4
4.CredSSP: client Role Client -DelegateComputer ServerB, ServerB Role Server
- Step 5
5.ServerB reaches ServerC as you: access OK
- Step 6
6.Risk: your credentials are cached on ServerB
Enable-WSManCredSSP -Role Client -DelegateComputer Server02.fabrikam.comEnable-WSManCredSSP -Role Server # on ServerBGet-WSManCredSSPDisable-WSManCredSSP -Role Clientcfg : http://schemas.microsoft.com/wbem/wsman/1/config/client/authlang : en-USBasic : trueDigest : trueKerberos : trueNegotiate : trueCertificate : trueCredSSP : truePS> Get-WSManCredSSPThe machine is configured to allow delegating fresh credentials to the following target(s): wsman/server02.accounting.fabrikam.com(not configured:)The machine is not configured to allow delegating fresh credentials.- Use an FQDN for -DelegateComputer; a wildcard like *.fabrikam.com delegates to more computers than needed.
- Default: CredSSP disabled on both sides.
237.10 WSMan connections and instances
Connect-WSMan -ComputerName "server01"cd WSMan:dirDisconnect-WSMan -ComputerName server01Get-WSManInstance -ResourceURI wmicimv2/Win32_Service -SelectorSet @{name="winrm"} -ComputerName "Server01"Get-WSManInstance winrm/config/listener -EnumerateWSManConfig: Microsoft.WSMan.Management\WSMan::WSManComputerName Type------------ ----localhost Containerserver01 Container PS> Get-WSManInstance winrm/config/listener -Enumeratecfg : http://schemas.microsoft.com/wbem/wsman/1/config/listenerSource : GPOAddress : *Transport : HTTPPort : 5985Enabled : trueURLPrefix : wsmanListeningOn : {}- Connect-WSMan adds a remote computer to the WSMan: drive next to localhost; across domains/workgroups give -Credential.
- Get-WSManInstance reads a resource by -ResourceURI; -SelectorSet picks the instance, -Fragment one property, -Enumerate all.
- An empty ListeningOn on a GPO listener means the policy configuration is wrong.
237.11 Linux equivalent lab (ssh + tmux)
Docker lab on 192.168.200.0/24: server .10 (sshd on port 22), client1 .21, client2 .22; each private key created only inside its own container.
ssh -o BatchMode=yes -o StrictHostKeyChecking=accept-new -i /root/.ssh/id_lab \ student@192.168.200.10 'hostname; whoami; uptime'Warning: Permanently added '192.168.200.10' (ED25519) to the list of known hosts.serverstudent 17:03:57 up 16:38, 0 users, load average: 1.10, 0.99, 0.73# client1: job.sh in tmux, ssh cut after 4 sTERM=xterm timeout 4 ssh -tt $SSHO student@192.168.200.10 'tmux new-session -s ittask sh job.sh'ssh $SSHO student@192.168.200.10 'tmux ls'# client2: another machine reads the same sessionssh $SSHO student@192.168.200.10 'tmux ls; tmux capture-pane -p -t ittask | grep -v ^$ | tail -3'# client2: close it (like Remove-PSSession)ssh $SSHO student@192.168.200.10 'tmux kill-session -t ittask; tmux ls'ssh ended with 255[client1]$ ssh ... 'tmux ls'ittask: 1 windows (created Mon Nov 9 17:04:00 2026)[client2]$ ... tail -3step 10step 11step 12[client2]$ ... tail -3 (a bit later)step 19step 20job done[client2]$ ssh ... 'tmux kill-session -t ittask; tmux ls'no server running on /tmp/tmux-1000/default| PowerShell Remoting | Linux equivalent |
|---|---|
| Invoke-Command -ComputerName | ssh host 'command' |
| Disconnect-PSSession | ssh connection cut, tmux session keeps running |
| Connect-PSSession from another PC | ssh from client2 + tmux capture-pane |
| Remove-PSSession | tmux kill-session |
237.12 Common mistakes
- Trying to disconnect an Enter-PSSession session: interactive sessions cannot be disconnected; use New-PSSession + Invoke-Command.
- Using an IP in -ComputerName and expecting Kerberos: Kerberos does not support IPs, so NTLM is used and needs HTTPS or TrustedHosts plus -Credential.
- Assuming Disable-PSRemoting undoes everything.
- Enabling CredSSP with a wildcard on untrusted servers.
237.13 Interview tips
- Enable-PSRemoting on the target creates the WinRM listener + firewall exception; client connects on 5985 (HTTP) or 5986 (HTTPS).
- In a domain Kerberos authenticates; results come back as serialized objects.
- Long work: New-PSSession, Invoke-Command, Disconnect-PSSession, later Connect-PSSession from any PC.
- Double hop: CredSSP works but caches credentials on the middle server.